Android对话框-更改数据
我有一个MainActivity,其中有一个按钮可以打开一个对话框。单击此按钮后,我打开了对话框框架,现在此对话框中有两个文本字段和两个按钮 如何通过单击其中一个按钮来更改该字段中的文本 我尝试了一些技巧,但我总是遇到Android对话框-更改数据,android,layout,view,android-activity,dialog,Android,Layout,View,Android Activity,Dialog,我有一个MainActivity,其中有一个按钮可以打开一个对话框。单击此按钮后,我打开了对话框框架,现在此对话框中有两个文本字段和两个按钮 如何通过单击其中一个按钮来更改该字段中的文本 我尝试了一些技巧,但我总是遇到NullPointerException 提前谢谢 使用AlertDialog.Builder创建对话框时,将按钮的onClickListener设置为更改EditText的值: AlertDialog.Builder dialog = new AlertDialog.Builer
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");
}
});
...
我会使用自定义对话框 您需要使用dialog对象来查找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>
然后
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?您已经有了EditText的引用,因为您在将其添加到对话框之前通过编程创建了它。如果不是以编程方式创建的,而是在XML布局中创建的,则使用
findViewById(id)
其中id是布局中的名称。@devid您需要使用dialog对象初始化EditText。您可以findViewById
将当前视图层次结构设置为活动。在你的情况下,你有对话。所以您需要使用dialog对象初始化edittext。@dejvid检查我的编辑。它应该会起作用。根据您的需要进行修改
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
}