Android 如何从AlertDialog获取视图?
我以这种方式构建alertDialog:Android 如何从AlertDialog获取视图?,android,android-layout,android-alertdialog,Android,Android Layout,Android Alertdialog,我以这种方式构建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
<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>
但是这样做我会收到一个NullPointerException
我用了这个:
(EditText)((Dialog) dialog).findViewById(R.id.username);
在早期应用程序中,我在单击后获取值。
在这种情况下,我没有对象对话框接口对话框。所以我不能用它。如何填写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();
}
Builder myCustomDialog = new AlertDialog.Builder(yourContext);
LayoutInflater factory = LayoutInflater.from(context);
final View yourInflatedView = factory.inflate(R.layout.your_xml_layout, null);
//Here you get your imageView (or TextView, EditText,...)
ImageView img1 = (ImageView) yourInflatedView.findViewById(R.id.img1);
// Do stuff with your imageview...
myCustomDialog.setView(yourInflatedView);
myCustomDialog.show();
我有一个简单的按钮,当在onClick()方法中时
createDialog();
带有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();
这是为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();
}
Builder myCustomDialog = new AlertDialog.Builder(yourContext);
LayoutInflater factory = LayoutInflater.from(context);
final View yourInflatedView = factory.inflate(R.layout.your_xml_layout, null);
//Here you get your imageView (or TextView, EditText,...)
ImageView img1 = (ImageView) yourInflatedView.findViewById(R.id.img1);
// Do stuff with your imageview...
myCustomDialog.setView(yourInflatedView);
myCustomDialog.show();
也可以通过以下方式创建对话框:
public void displayPopup(Context ctx) {
Dialog cd_display = new Dialog(ctx);
cd_display.requestWindowFeature(Window.FEATURE_NO_TITLE);
cd_display.setContentView(R.layout.alert_dialog_layout);
ImageView n1Dialog = (ImageView) cd_display.findViewById(R.id.img1);
cd_display.show();
}
显示对话框完成代码发布您的代码。如何调用alertdialog。我使用以下dialogObject编辑了问题。findViewById(R.id.img1)@萨亚尼不工作!对象alertDialog是这样初始化的:alertDialog.Builder alertDialog;而且findViewById不存在于该类中,谢谢您的回答!我看了一遍答案,但我还是要谢谢你们!我很抱歉!我看到我没有更多的NullPointerException,所以我认为它是正确的!谢谢你的回答!我没有尝试你的解决方案,因为我在另一个系统中修复了。谢谢