简体   繁体   English

自定义布局按钮在AlertDialog中不起作用

[英]Custom Layout button is not working in an AlertDialog

I've been trying to fix this for a while now including searching online but I just can't find solution. 我已经尝试解决此问题一段时间了,包括在线搜索,但我只是找不到解决方案。 Everyone saying you should add onClickListener and I did but it just does not work. 每个人都说您应该添加onClickListener ,但我确实这样做了,但它根本无法正常工作。 When I click the button nothing happens. 当我单击按钮时,什么也没有发生。 No verbose log ever appears. 不会显示详细日志。 This is all in one class which implements Runnable so it can do stuff in the background and this EditText onClickListener is implemented inside onCreate of a class. 所有这些都在一个实现Runnable的类中,因此它可以在后台执行操作,并且此EditText onClickListener在类的onCreate中实现。

  //Setting up EditText onClick Listeners
        oof.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Creating custom layout dialog
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                final View inflatedView = getLayoutInflater().inflate(R.layout.dialog_box_popup, null);
                builder.setView(R.layout.dialog_box_popup);
                builder.setTitle("Edit");

                final EditText indc = (EditText)inflatedView.findViewById(R.id.weightInDcEtxt);
                Button incrementBtn = (Button)inflatedView.findViewById(R.id.incrementBtn);
                Button decrementBtn = (Button)inflatedView.findViewById(R.id.decrementBtn);

                //Enabling the increment button in a dialog
                incrementBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Log.v("increment", "Button clicked: " + MiscVariables.rocket);
                    }
                });

                //Displaying dialog
                AlertDialog dialog = builder.create();  
                dialog.show();   
            }
        });

And here is the custom layout for the dialog using ConstraintLayout: 这是使用ConstraintLayout的对话框的自定义布局:

<?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="match_parent">

    <Button
        android:id="@+id/decrementBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:text="-"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/incrementBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="+"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/weightInDcEtxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:ems="10"
        android:gravity="center"
        android:inputType="numberDecimal"
        android:text="@string/editTextFromDialog"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/incrementBtn"
        app:layout_constraintStart_toEndOf="@+id/decrementBtn"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayou

From there I have no idea what to do. 从那里我不知道该怎么办。 If anyone can help me out I would appreciate it thanks! 如果有人可以帮助我,我将非常感谢!

I've found a solution. 我找到了解决方案。 Here is the code: 这是代码:

//Setting up EditText onClick Listeners
        oof.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Creating custom layout dialog
                AlertDialog.Builder builder = new AlertDialog.Builder(context);        
                builder.setView(R.layout.dialog_box_popup);
                builder.setTitle("Edit");

                //Enabling the increment button in a dialog
                incrementBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Log.v("increment", "Button clicked: " + MiscVariables.rocket);
                    }
                });

                //Displaying dialog
                AlertDialog dialog = builder.create();
                dialog.show();

                final EditText indc = 
                (EditText)inflatedView.findViewById(R.id.weightInDcEtxt);
                Button incrementBtn = 
                (Button)dialog.findViewById(R.id.incrementBtn);
                Button decrementBtn = 
                (Button)dialog.findViewById(R.id.decrementBtn); 
            }

        });

Inflated view was useless here. 夸大的观点在这里是没有用的。 All had to be accessed by doing dialog.findViewById. 必须通过执行dialog.findViewById.来访问所有dialog.findViewById.

  final View inflatedView = View.inflate(getActivity(), R.layout.dialog_box_popup, null);
            AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
            alert.setView(inflatedView);

            final EditText indc = (EditText) inflatedView.findViewById(R.id.weightInDcEtxt);
            Button incrementBtn = (Button) inflatedView.findViewById(R.id.incrementBtn);
            Button decrementBtn = (Button) inflatedView.findViewById(R.id.decrementBtn);


            //Enabling the increment button in a dialog
            incrementBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Toast.makeText(getActivity(), "Hellloo", Toast.LENGTH_SHORT).show();
                }
            });

            alert.show();

Try this, Hope this solution will be work...!!! 试试这个,希望这个解决方案能起作用... !!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM