반응형
'확인'버튼으로 메시지 상자를 추가하는 방법은 무엇입니까?
확인 버튼이있는 메시지 상자를 표시하고 싶습니다. 다음 코드를 사용했지만 인수와 함께 컴파일 오류가 발생합니다.
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("This is an alert with no consequence");
dlgAlert.setTitle("App Title");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
Android에서 메시지 상자를 표시하려면 어떻게해야합니까?
확인 긍정 버튼에 대한 클릭 리스너를 추가하지 않은 문제가있을 수 있습니다.
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
귀하의 상황에서는 짧고 간단한 메시지로만 사용자에게 알리고 싶기 때문에 Toast
a는 더 나은 사용자 경험을 제공합니다.
Toast.makeText(getApplicationContext(), "Data saved", Toast.LENGTH_LONG).show();
업데이트 : 플로팅 작업은 재료 설계 응용 프로그램에 대한 토스트 대신 이제 좋습니다.
독자가 읽고 이해할 수있는 시간을주고 싶은 더 긴 메시지가있는 경우 DialogFragment
. ( 현재 문서는AlertDialog
직접 호출하는 대신 조각으로 래핑하는 것을 권장합니다 .)
확장하는 클래스 만들기 DialogFragment
:
public class MyDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("App Title");
builder.setMessage("This is an alert with no consequence");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// You don't have to do anything here if you just
// want it dismissed when clicked
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
그런 다음 활동에서 필요할 때 호출하십시오.
DialogFragment dialog = new MyDialogFragment();
dialog.show(getSupportFragmentManager(), "MyDialogFragmentTag");
또한보십시오
코드는 나를 위해 잘 컴파일됩니다. 가져 오기를 추가하는 것을 잊었을 수 있습니다.
import android.app.AlertDialog;
@Override
protected Dialog onCreateDialog(int id)
{
switch(id)
{
case 0:
{
return new AlertDialog.Builder(this)
.setMessage("text here")
.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0, int arg1)
{
try
{
}//end try
catch(Exception e)
{
Toast.makeText(getBaseContext(), "", Toast.LENGTH_LONG).show();
}//end catch
}//end onClick()
}).create();
}//end case
}//end switch
return null;
}//end onCreateDialog
참고 URL : https://stackoverflow.com/questions/6264694/how-to-add-message-box-with-ok-button
반응형
'Nice programing' 카테고리의 다른 글
Perm 공간과 힙 공간 (0) | 2020.10.12 |
---|---|
“비 복합 이름이있는 use 문은… 효과가 없습니다.”문제 해결 (0) | 2020.10.12 |
div를 "탭 가능"으로 만드는 방법은 무엇입니까? (0) | 2020.10.12 |
Docker는 7000에서 8000까지의 모든 포트 또는 포트 범위를 노출합니다. (0) | 2020.10.12 |
이 구조는 어떻게 sizeof == 0을 가질 수 있습니까? (0) | 2020.10.12 |