How to get reference to a Button created in a custom Dialog, using a xml layout?(如何使用 xml 布局获取对在自定义对话框中创建的按钮的引用?)
问题描述
我有一个活动,用户可以在其中更新特定信息,点击标签附近的按钮.这个按钮会触发一个对话框,其中我有一些字段来获取用户输入和一个按钮来完成编辑.
I have an Activity in which user can update a specific information clicking in a button near a label. This buttons than triggers a Dialog where I have some fields to get user input and a button to finish editing.
我的问题是我无法在对话框特定的 xml 布局中获得对按钮声明的引用.按钮引用返回 null.按照一些代码片段来说明.
My problem is that I am not able to get a reference to the button declare in the dialog specific xml layout. The button reference returns null. Follow some code snippet to ilustrate.
触发事件以构建对话框的按钮在活动中声明为实例变量,如下所示:
Button which fires the event to build the dialog is declared as a instance variable in the activity as follows:
private Button bConfigurarCarro;
比onCreate方法:
than onCreate method:
bConfigurarCarro = (Button)findViewById(R.id.bConfigurarCarro);
bConfigurarCarro.setOnClickListener(configuraCarroListener);
这会正确触发事件以创建对话框:
this correctly fires the event to create the dialog:
protected OnClickListener configuraCarroListener = new OnClickListener(){
public void onClick(View v) {
showDialog(CARRO_DIALOG_ID);
Log.d(TAG, "Executando evento do botão de configuração de carro no abastecimento.");
}
};
比创建对话框重写 onCreateDialog 方法是这样的:
than to create the dialog Overrides onCreateDialog method like this:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this, mTimeSetListener, hora, minuto, false);
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, ano, mes, dia);
case CARRO_DIALOG_ID:
Log.d(TAG, "Criando dialog de cadastro de carro.");
dialogCarro = new Dialog(this);
dialogCarro.setContentView(R.layout.novo_carro_dialog);
bSalvarCarro = (Button)findViewById(R.id.botaoSalvarCarro);
bSalvarCarro.setOnClickListener(salvarCarroListener);
dialogCarro.setTitle("Carro");
dialogCarro.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
Log.d(TAG, "Dialog de cadastro de carro criado retornando...");
return dialogCarro;
}
return null;
}
NullPointer 触发的特定行是在尝试获取上面的 Button 引用后,我尝试 setOnClickListener..
The specific line where the NullPointer triggers is the one where after trying to get Button reference above, I than try to setOnClickListener..
bSalvarCarro = (Button)findViewById(R.id.botaoSalvarCarro);
bSalvarCarro.setOnClickListener(salvarCarroListener);
bSalvarCarro 为空.
the bSalvarCarro is null.
我尝试使用代码行在上面设置的对话框的 xml 布局:
The xml layout for the Dialog which I try to set above using the line of code:
dialogCarro.setContentView(R.layout.novo_carro_dialog);
是这个吗(novo_carro_dialog.xml):
Is this one(novo_carro_dialog.xml):
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TableRow>
<TextView android:id="@+id/marcaCarro"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/marcaCarro"/>
<EditText android:id="@+id/tMarcaCarro" android:singleLine="true" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:width="130px"/>
</TableRow>
<TableRow>
<TextView android:id="@+id/nomeCarro"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/nomeCarro"/>
<EditText android:id="@+id/tNomeCarro" android:singleLine="true" android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView android:id="@+id/anoCarro"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/anoCarro"/>
<EditText android:id="@+id/tAnoCarro" android:singleLine="true" android:layout_width="fill_parent"
android:numeric="integer" android:maxLength="4" android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<TextView android:id="@+id/modeloCarro"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="@string/modeloCarro"/>
<EditText android:id="@+id/tModeloCarro" android:singleLine="true" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</TableRow>
<Button android:id="@+id/botaoSalvarCarro" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/bSalvarCarro" />
</TableLayout>
如您所见,Button 是用 id botaoSalvarCarro 声明的,但尝试获取对它的引用会返回 null.我对此有点困惑,好像我取出设置侦听器的行,对话框正确显示,所以.如何正确获取对该按钮的引用?
As you can see the Button is declared with the id botaoSalvarCarro, but trying to get a reference to it returns null. I am a bit confused by this as if I take out the line which sets the listener the dialog is correctly showed, so. How do I get the reference to this button correctly?
推荐答案
dialogCarro = new Dialog(this);
dialogCarro.setContentView(R.layout.novo_carro_dialog);
bSalvarCarro = (Button)dialogCarro.findViewById(R.id.botaoSalvarCarro);
这篇关于如何使用 xml 布局获取对在自定义对话框中创建的按钮的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 xml 布局获取对在自定义对话框中创建的按钮的引用?
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01