我有MainActivity,我有一个打开对话框的按钮。一旦我点击这个按钮,我打开了我的对话框,现在我在这个对话框中有两个文本字段和两个按钮。Android对话框 - 更改数据
如何通过单击其中一个按钮来更改该字段中的文本?
我正在尝试一些技巧,但我总是得到NullPointerException
。
在此先感谢!
我有MainActivity,我有一个打开对话框的按钮。一旦我点击这个按钮,我打开了我的对话框,现在我在这个对话框中有两个文本字段和两个按钮。Android对话框 - 更改数据
如何通过单击其中一个按钮来更改该字段中的文本?
我正在尝试一些技巧,但我总是得到NullPointerException
。
在此先感谢!
当您使用AlertDialog.Builder的对话框中,设置按钮的onClickListener改变EditText上的值:
AlertDialog.Builder dialog = new AlertDialog.Builer(this);
...
final EditText edit = new EditText(this);
dialog.setView(edit);
...
dialog.setPositiveButton("Change the freakin text",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id){
edit.setText("bla bla bla");
}
});
...
我会用一个自定义对话框。
您需要使用对话框对象来查找ViewById并初始化视图。
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignParentRight="true"
android:layout_marginRight="58dp"
android:text="Cancel" />
<EditText
android:id="@+id/ed1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="56dp"
android:layout_toLeftOf="@+id/button2"
/>
<EditText
android:id="@+id/ed2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginRight="27dp"
android:layout_marginTop="39dp"
android:text="ChangeText" />
</RelativeLayout>
然后在你的活动上说点击链接
dialogPopup();
然后
public void dialogPopup()
{
final Dialog d = new Dialog(MainActivity.this); // initialize dialog
d.setContentView(R.layout.dialog);
d.setTitle("my title");
final EdiText ed1= (TextView) d.findViewById(R.id.ed1);
// initialize edittext using the dialog object.
final EdiText ed2= (TextView) d.findViewById(R.id.ed2);
Button b = (Button) d.findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() // button 1 click
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// on click of button 1 change the text in textview's
ed1.setText("Changing text 1");
ed2.setText("Changing text 2");
}
});
Button b1 = (Button) d.findViewById(R.id.button2); // button2 click
b1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
d.dismiss(); // dismiss dialog
}
});
d.show(); // show the dialog
}
但我已经创建的EditText字段随机文本,我想通过点击一个按钮来改变它。如何获取放置在对话框布局中的EditText字段的ID? – dejvid
您已经拥有EditText的引用,因为您在将它添加到对话框之前以编程方式创建了它。如果尚未以编程方式创建它,但是在XML布局中,则使用'findViewById(id)',其中id是布局中的名称。 –
@dejvid你需要使用dialog对象来初始化editext。您可以将当前视图层次结构的'findViewById'设置为该活动。在你的情况下,你有对话。所以你需要使用dialog对象来初始化edittext。 – Raghunandan