Programing

Android 단순 알림 대화 상자

lottogame 2020. 6. 29. 08:02
반응형

Android 단순 알림 대화 상자


이 질문에는 이미 답변이 있습니다.

내 안드로이드 앱에서 버튼을 클릭하는 사용자에게 작은 문자 메시지를 표시해야합니다 .IOS에서는 사용하기 간단하지만 안드로이드에서는 솔루션이 x10 배 더 어려워 보이기 때문에 고심하고있는 AlertView를 만들어야했습니다. DialogFragment를 사용해야한다는 것을 알았지 만 작동 방법을 이해할 수 없습니다. 누군가 설명 할 수 있습니까? 또한 내 솔루션이 맞습니까? 아니면 사용자에게 간단한 문자 메시지를 표시하기가 더 쉽습니까?


당신은 단순히 당신 의이 작업을 수행해야합니다 onClick:

AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("Alert message to be shown");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
alertDialog.show();

단순히 경고를 표시하기 위해 DialogFragment가 필요하다는 것을 어디에서 보았는지 알 수 없습니다.

도움이 되었기를 바랍니다.


내 친구는 매우 간단하지 않으므로 다음을 사용하십시오.

AlertDialog alertDialog = new AlertDialog.Builder(AlertDialogActivity.this).create();
alertDialog.setTitle("Alert Dialog");
alertDialog.setMessage("Welcome to dear user.");
alertDialog.setIcon(R.drawable.welcome);

alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
    }
});

alertDialog.show();

학습서 는 xml을 사용하여 사용자 정의 대화 상자를 작성한 후이를 경고 대화 상자로 표시하는 방법을 보여줍니다.


자신 만의 'AlertView'를 쉽게 만들어 어디서나 사용할 수 있습니다.

alertView("You really want this?");

한 번 구현하십시오.

private void alertView( String message ) {
 AlertDialog.Builder dialog = new AlertDialog.Builder(context);
 dialog.setTitle( "Hello" )
       .setIcon(R.drawable.ic_launcher)
       .setMessage(message)
//     .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
//      public void onClick(DialogInterface dialoginterface, int i) {
//          dialoginterface.cancel();   
//          }})
      .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialoginterface, int i) {   
        }               
        }).show();
 }

참고 URL : https://stackoverflow.com/questions/26097513/android-simple-alert-dialog

반응형