java findViewById响应“无”

qgzx9mmu  于 2023-02-18  发布在  Java
关注(0)|答案(1)|浏览(110)

我正在尝试从AlertDialog的EditText视图中获取文本()。在方法onCreate()的主活动中,我设置了按钮侦听器。

public class MainActivity2 extends AppCompatActivity {

    private ActivityMain2Binding mainActivity2;
    private ArrayAdapter adapter;
    private ArrayList<simpleArrayAdapterElement> data;
    private final static String KEY_TITLE = "title";
    private final static String KEY_DESCRIPTION = "description";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainActivity2 = ActivityMain2Binding.inflate(getLayoutInflater());
        View view = mainActivity2.getRoot();
        setContentView(view);
        arrayAdapter();
        mainActivity2.buttonIddItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCustomDialog();
            }
        });
//        setupListViewSimple();
    }

然后我在onClick中调用showCustomDialog()。

private void showCustomDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(R.layout.custom_dialog);
        builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                EditText alertDialogText = (EditText) findViewById(R.id.setText1);
                System.out.println(alertDialogText.getText());
                System.out.println(alertDialogText.getText().toString()); // just in case
            }
        });
        builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

编辑文本警报对话框文本=(编辑文本)查找视图按ID(R.id.setText1);//不返回任何内容
下面是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <EditText
        android:id="@+id/setText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints"
        android:layout_marginTop="16dp"
        android:hint="Type your name here:"
        android:text="Some"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
  • 我也在我的活动中使用绑定。

我做错了什么?

iklwldmw

iklwldmw1#

您正在引用MainActivity2本身。您需要使用LayoutInflater引用其视图。您可以在此处查看示例答案:https://stackoverflow.com/a/36793463/9346054

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

    LayoutInflater inflater = this.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.custom_dialog, null);

    builder.setView(dialogView);
    builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            EditText alertDialogText = dialogView.findViewById(R.id.setText1);
            System.out.println(alertDialogText.getText());
            System.out.println(alertDialogText.getText().toString()); // just in case
        }
    });
    builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

相关问题