0

当我向 alertDialog 添加布局按钮时。它不显示任何内容,它强制停止应用程序。如何为 AlertDialog 中的按钮设置 onClickListener。我想在按钮按下时显示一个时间选择器

         LayoutInflater li = LayoutInflater.from(this);
        final View promptsView = li.inflate(R.layout.prompts, null);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setView(promptsView);
        final EditText userInput = (EditText) promptsView
                .findViewById(R.id.editTextDialogUserInput);

        startTimeButton=(Button)promptsView.findViewById(R.id.buttonStartTime);
        stopTimeButton=(Button)promptsView.findViewById(R.id.buttonStopTime);
        startTimeButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "btn clocled", Toast.LENGTH_LONG).show();
               // showDialog(TIME_DIALOG_ID);
            }
        });

        stopTimeButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "btn clicled", Toast.LENGTH_LONG).show();
               // showDialog(TIME_DIALOG_ID);
            }
        });

        alertDialogBuilder.setCancelable(false).setPositiveButton("OK",
                new DialogInterface.OnClickListener()
                {
            public void onClick(DialogInterface dialog,int which)
            {

                   final RadioGroup radioGroup = (RadioGroup)promptsView.findViewById(R.id.radioGroup1);
                   int checkedRadioButton = 0;
                   try {
                       checkedRadioButton = radioGroup.getCheckedRadioButtonId();
                   } catch (Exception e) {
                       e.printStackTrace();
                   }
                   int i=0;
                   toggleLogging(AppSettings.getServiceRunning(MainActivity.this),
                        AppSettings.setLoggingInterval(MainActivity.this,currentIntervalChoice));
                 dialog.dismiss();
                // return;  
            }
              });
        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();
        // show it
        alertDialog.show();

          like this:
              <Button
    android:id="@+id/buttonStartTime"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:text="Set Start Time" />

<TextView
    android:id="@+id/StartTime"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="startTime"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<Button
    android:id="@+id/buttonStopTime"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:text="Set Stop Time" />        
4

4 回答 4

0
Obj.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // if this button is clicked, close

                }
            });
于 2013-05-13T05:19:03.700 回答
0

试试这个代码:

public void OkCancleAlertDialog(final Context appContext, String title,
        final String msg) {
    try {
        new AlertDialog.Builder(appContext)
                .setTitle(title)
                .setMessage(msg)
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {


                            }
                        })

                .setNegativeButton("No",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {
                            }
                        }).show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
于 2013-05-13T05:24:25.070 回答
0

试试这个代码......

public void diallog(String title,String message){
    AlertDialog.Builder WAlert = new  AlertDialog.Builder(MainActivity.this);
    WAlert
          .setTitle(title)
          .setMessage(message)
          .setPositiveButton("Ok!", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub          
            }
        })
    .create()
    .show();
}

这是一个多用途的功能......你只需要用你的味精调用它。

喜欢:

dialog("Alert","Enter value first.");
于 2013-05-13T06:19:03.110 回答
0

像这样设置警报对话框按钮

DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

       // private ParseObject current_user;
        ParseUser current_user = ParseUser.getCurrentUser();
        int points;
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which){
            case DialogInterface.BUTTON_POSITIVE:

              //code for this button
                break;
            case DialogInterface.BUTTON_NEUTRAL:

                //code for this button

                break;

            case DialogInterface.BUTTON_NEGATIVE:

                dialog.dismiss();


                break;
            }
        }
    };

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("title");
     builder.setMessage("You don't have function")
     .setPositiveButton("", dialogClickListener)
     .setNeutralButton("", dialogClickListener)
     .setNegativeButton("Cancle", dialogClickListener).show();
于 2013-05-13T05:24:32.923 回答