gpt4 book ai didi

android - 如何从 AlertDialog 获取 View ?

转载 作者:行者123 更新时间:2023-11-30 02:16:24 25 4
gpt4 key购买 nike

我用这种方式构建了一个 alertDialog:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/title1"
android:textSize="20sp"
android:textColor="@color/white" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/chars1" />

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/RelativeLayout1"
android:background="@drawable/body_white">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Layout1"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/img1"
android:id="@+id/img1"
android:layout_alignParentTop="true"
/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img2"
android:background="@drawable/img2"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/img1"
android:layout_toEndOf="@+id/img1" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>

我想做这件事:

img1 = (ImageView) findViewById(R.id.img1);

但是这样做我会收到一个 NullPointerException

我用过这个:

(EditText)((Dialog) dialog).findViewById(R.id.username);

在我点击后获取值(value)的早期应用程序中。在这种情况下,我没有对象 DialogInterface 对话框。所以我不能使用它。如何填充 img1?

我想手动设置填充,但我想在 JAVA 中而不是在 XML 中执行此操作。

谢谢

编辑:

这是启动 alertDialog 的方法:

public void createDialog() {
alertDialog = new AlertDialog.Builder(Instructions.this);
LayoutInflater inflater = this.getLayoutInflater();
alertDialog.setView(inflater.inflate(R.layout.alert_dialog_layout, null));
n1Dialog = (ImageView) findViewById(R.id.img1);
n1Dialog.setPadding(0,0,0,0);
alertDialog.show();

}

我有一个简单的按钮,在 onClick() 方法中有

createDialog();

Row with setPadding(那只是一个证明,这不是我想做的)是错误的,因为是 NullPointerException

最佳答案

这样做:

 alertDialog = new AlertDialog.Builder(Instructions.this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView=inflater.inflate(R.layout.alert_dialog_layout, null);
alertDialog.setView(dialogView);
n1Dialog = (ImageView) dialogView.findViewById(R.id.img1);
n1Dialog.setPadding(0,0,0,0);
alertDialog.show();

关于android - 如何从 AlertDialog 获取 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29276539/

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