在AlertDialog中实现按钮

时间:2013-05-13 05:12:21

标签: android android-dialog

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

         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 个答案:

答案 0 :(得分:0)

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

                }
            });

答案 1 :(得分: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();
    }
}

答案 2 :(得分: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();

答案 3 :(得分: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.");