gpt4 book ai didi

android - 查找 AlertDialog 的 View

转载 作者:搜寻专家 更新时间:2023-11-01 07:46:26 24 4
gpt4 key购买 nike

我想从 AlertDialog 获取 View :

Button R = (Button) findViewById(R.id.toggleButtonA);

但如果我这样做,它总是为空。如何获取和编辑 View ?

以下是代码的相关部分:

什么创建了对话框:

public void openSelection(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);
LayoutInflater inflater = this.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialayout, null));
AlertDialog dialog = builder.create();
dialog.show();
}

dialayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">

<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/toggleButtonA"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:onClick="select"
android:text="All"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toggleButtonM" />


</android.support.constraint.ConstraintLayout>

</LinearLayout>

最佳答案

您需要先将膨胀 View 分配给一个变量。示例:

public void openSelection(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
View v = inflater.inflate(R.layout.activity_main, null); // this line
builder.setView(v);
AlertDialog dialog = builder.create();
dialog.show();

Button RR = (Button) v.findViewById(R.id.toggleButtonA); // this is how you get a view of the button
}

请将您的按钮变量名 R 重命名为另一个名称,可能是因为 R 是保留字。

关于android - 查找 AlertDialog 的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43313116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com