AlertBuilder上的按钮onClickListener不起作用

时间:2019-09-04 04:27:44

标签: android android-activity

我有一个要在AlertBuilder中调用的活动和布局, 我在布局中有3个按钮,每个按钮都有一些代码。 问题是,当我创建并显示警报时,onClickListener无法正常工作

我不想使用提醒按钮,而不得不在布局上使用我的按钮

MainActivity代码(通话提醒)

LayoutInflater inflater = (MainActivity.this).getLayoutInflater();
builder.setTitle("Login Form");

builder.setCancelable(false);
builder.setView(inflater.inflate(R.layout.activity_login_prompt, null));
builder.create();
builder.show();

LoginPromptActivity

public class LoginPromptActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_prompt);
    Button bLogin = (Button)findViewById(R.id.btnLogin);
    bLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(LoginPromptActivity.this, "Button Clicked",  Toast.LENGTH_SHORT).show();
        }

    });
}
}

布局XML代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".LoginPromptActivity"
tools:showIn="@layout/activity_login_prompt">


<ImageView
    android:id="@+id/imageView2"
    android:layout_width="192dp"
    android:layout_height="147dp"
    android:layout_marginStart="32dp"
    android:layout_marginLeft="32dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="32dp"
    android:layout_marginRight="32dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/login" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="32dp"
    android:layout_marginRight="32dp"
    android:text="Username"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView2" />

<EditText
    android:id="@+id/txtUserName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="32dp"
    android:layout_marginLeft="32dp"
    android:layout_marginTop="17dp"
    android:layout_marginEnd="32dp"
    android:layout_marginRight="32dp"
    android:ems="10"
    android:inputType="textPersonName"
    app:layout_constraintEnd_toStartOf="@+id/textView4"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView2" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="32dp"
    android:layout_marginRight="32dp"
    android:text="Password"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView4" />

<EditText
    android:id="@+id/txtPassword"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:ems="10"
    android:inputType="textPassword"
    app:layout_constraintEnd_toStartOf="@+id/textView5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/txtUserName" />

<Button
    android:id="@+id/btnLogin"
    android:layout_width="127dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:text="Login"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/txtPassword" />

<Button
    android:id="@+id/btnRegister"
    android:layout_width="94dp"
    android:layout_height="46dp"
    android:layout_marginTop="32dp"
    android:text="Register"
    app:layout_constraintEnd_toStartOf="@+id/btnLogin"
    app:layout_constraintTop_toBottomOf="@+id/txtPassword" />

<Button
    android:id="@+id/btnCancel"
    android:layout_width="126dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:text="Exit App"
    app:layout_constraintEnd_toStartOf="@+id/btnRegister"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/txtPassword" />

</android.support.constraint.ConstraintLayout>

4 个答案:

答案 0 :(得分:1)

我已经阅读了您的问题,我认为您在实现onclick侦听器时遇到了一些问题 在警报对话框按钮上。如果我们对警报对话框使用了任何自定义布局,则不需要任何活动来处理此布局。根据您的代码sinppet,您已经使用自定义布局创建了一个警告对话框,并使用一个活动来处理此布局按钮,单击

尝试使用此代码创建自定义警报对话框,并处理其单击侦听器

  AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setTitle("Login Form");
    View view= getLayoutInflater().inflate(R.layout.dialog_custom, null);
    alertDialogBuilder.setCancelable(false);
    alertDialogBuilder.setView(view);

    alertDialogBuilder.create();
     //alert dialog button 
    TextView ok_btn=view.findViewById(R.id.ok_btn);
    //buttton click
   ok_btn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            Toast
               .makeText(MainActivity.this, "Hello", Toast.LENGTH_SHORT)
               .show();
        }
    });
    alertDialogBuilder.show();

答案 1 :(得分:0)

 private void showAlertDialog(){
@SuppressLint("InflateParams") final View promptView = LayoutInflater.from(mainActivity).inflate(R.layout.enter_pin_dialog, null);    

 Button accept= (Button) promptView.findViewById(R.id.accept_btn);
    Button deny= (Button) promptView.findViewById(R.id.deny_btn);    

 deny.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           // do what you want
        }
    });    

accept.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           // do what you want
        }
    });    


 NotificationUtil.messageDialog(
            mainActivity,
            null,
            null,
            null,
            false,
            true,
            promptView,
            null,
            null,
            null,
            null,
            null,
            null
    );}    

public class NotificationUtil {

private static ProgressDialog progressDialog;
private static AlertDialog cancelDialog;
private static AlertDialog messageDialog;
private static Vector<AlertDialog> dialogs= new Vector<AlertDialog>();

public static void messageDialog(Context context, String title, String message, Integer iconResId,
                                 boolean cancelable, boolean openKeyboard, View view,
                                 String positiveStr, DialogInterface.OnClickListener positiveListener,
                                 String negativeStr, DialogInterface.OnClickListener negativeListener,
                                 DialogInterface.OnShowListener showListener, DialogInterface.OnDismissListener dismissListener) {

    AlertDialog.Builder builder = new AlertDialog.Builder(context);

    builder.setTitle(title);
    builder.setMessage(message);
    builder.setCancelable(cancelable);

    if (iconResId != null) {
        builder.setIcon(iconResId);
    }

    if (view != null) {
        builder.setView(view);
    }

    if (positiveStr != null) {
        builder.setPositiveButton(positiveStr, positiveListener);
    }

    if (negativeStr != null) {
        builder.setNegativeButton(negativeStr, negativeListener);
    }

    messageDialog = builder.create();
    dialogs.add(messageDialog);

    if (openKeyboard) {
        Window window = messageDialog.getWindow();
        if (window != null) {
            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        }
    }

    if (showListener != null) {
        messageDialog.setOnShowListener(showListener);
    }

    if (dismissListener != null) {
        messageDialog.setOnDismissListener(dismissListener);
    }


    messageDialog.show();

    ScrollView buttonsContainer = (ScrollView) messageDialog.findViewById(R.id.buttonPanel);
    if (buttonsContainer != null) {
        List<View> views = new ArrayList<>();
        for (int i = 0; i < buttonsContainer.getChildCount(); i++) {
            views.add(buttonsContainer.getChildAt(i));
        }
        buttonsContainer.removeAllViews();
        for (int i = views.size() - 1; i >= 0; i--) {
            buttonsContainer.addView(views.get(i));
        }
    }
}}    

答案 2 :(得分:0)

尝试

AlertDialog.Builder alertbox = new AlertDialog.Builder(v.getRootView().getContext());
            alertbox.setMessage("ddddd");
            alertbox.setTitle("dddd");
            alertbox.setIcon(R.drawable.ic_del);

            alertbox.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface arg0,
                                            int arg1) {

                            }
                        }
                    });
            alertbox.setNegativeButton("No",new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface arg0,
                                    int arg1) {

                }
            });
            alertbox.show();

答案 3 :(得分:0)

打开MainActivity.java并将OnClickListener添加到您的按钮以创建警报对话框:

  btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Create a Custom Alert Dialog
            final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            LayoutInflater inflater = getLayoutInflater();
            View view = inflater.inflate(R.layout.list_layout,null);
            TextView tv = (TextView)view.findViewById(R.id.head);
            ImageView iv = (ImageView)view.findViewById(R.id.iv);
            builder.setView(view);
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // Dismiss the dialog here
                    dialog.dismiss();
                }
            });
            builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // Add ok operation here
                }
            });

            builder.show();


        }
    });

您还可以在警报对话框中添加自定义按钮。