无法在自定义对话框中更新 TextView 的文本
Not able to update TextView's text in custom dialog box
请帮忙。我什么都试过了,但每次都失败了。我正在构建一个 android 应用程序,我需要在其中以编程方式更新对话框中 TextView 的文本。
此对话框是使用自定义布局创建的。在下面发布代码,请帮助将 tv1 的值更新为 "It's the new text"。在 XML 布局文件中,文本设置为 "old text"。
在我运行这个程序之后,'dialogabt'中的tv1的值仍然被视为"old text"。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.b1);
//generating first dialog box
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Dialog dialog1 = new Dialog(MainActivity.this);
dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog1.setContentView(R.layout.setings);
dialog1.show();
}
});
}
}
生成第二个对话框并更新其中TextView的文本的函数
public void abtit(View view) {
Dialog dialogabt = new Dialog(this);
dialogabt.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.custom2, null);
TextView tv1 = (TextView) dialogView.findViewById(R.id.firstapp);
System.out.println("Going to set new value");
tv1.setText("Its new text");
System.out.println("done setting new value");
dialogabt.setContentView(R.layout.custom2);
dialogabt.show();
}
settings.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/set3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:onClick="abtit"
android:text="About app" />
</RelativeLayout>
custom2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="315dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/custom_alert2"
android:orientation="vertical">
<TextView
android:id="@+id/firstapp"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:onClick="openapp2"
android:text="old text" />
</RelativeLayout>
删除以下行,因为您是在将文本设置为 textView 之后设置 contentView
dialogabt.setContentView(R.layout.custom2);
并将此行添加到下方
View dialogView = inflater.inflate(R.layout.custom2, null);
试试这个:
dialogabt.setContentView(R.layout.custom2);
TextView tv1 = (TextView) dialogabt.findViewById(R.id.firstapp);
System.out.println("Going to set new value");
tv1.setText("Its new text");
System.out.println("done setting new value");
dialogabt.show();
检查您的 abtit() 方法,您在一个自定义视图中设置值,而在警报对话框中您设置另一个视图,因此它不会影响您拥有的值为 dialogView 对象设置。
所以只需更改下面的行,
dialogabt.setContentView(R.layout.custom2);
由
dialogabt.setView(dialogView);
它会起作用
请帮忙。我什么都试过了,但每次都失败了。我正在构建一个 android 应用程序,我需要在其中以编程方式更新对话框中 TextView 的文本。 此对话框是使用自定义布局创建的。在下面发布代码,请帮助将 tv1 的值更新为 "It's the new text"。在 XML 布局文件中,文本设置为 "old text"。 在我运行这个程序之后,'dialogabt'中的tv1的值仍然被视为"old text"。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.b1);
//generating first dialog box
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Dialog dialog1 = new Dialog(MainActivity.this);
dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog1.setContentView(R.layout.setings);
dialog1.show();
}
});
}
}
生成第二个对话框并更新其中TextView的文本的函数
public void abtit(View view) {
Dialog dialogabt = new Dialog(this);
dialogabt.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.custom2, null);
TextView tv1 = (TextView) dialogView.findViewById(R.id.firstapp);
System.out.println("Going to set new value");
tv1.setText("Its new text");
System.out.println("done setting new value");
dialogabt.setContentView(R.layout.custom2);
dialogabt.show();
}
settings.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/set3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:onClick="abtit"
android:text="About app" />
</RelativeLayout>
custom2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="315dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/custom_alert2"
android:orientation="vertical">
<TextView
android:id="@+id/firstapp"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:onClick="openapp2"
android:text="old text" />
</RelativeLayout>
删除以下行,因为您是在将文本设置为 textView 之后设置 contentView
dialogabt.setContentView(R.layout.custom2);
并将此行添加到下方
View dialogView = inflater.inflate(R.layout.custom2, null);
试试这个:
dialogabt.setContentView(R.layout.custom2);
TextView tv1 = (TextView) dialogabt.findViewById(R.id.firstapp);
System.out.println("Going to set new value");
tv1.setText("Its new text");
System.out.println("done setting new value");
dialogabt.show();
检查您的 abtit() 方法,您在一个自定义视图中设置值,而在警报对话框中您设置另一个视图,因此它不会影响您拥有的值为 dialogView 对象设置。
所以只需更改下面的行,
dialogabt.setContentView(R.layout.custom2);
由
dialogabt.setView(dialogView);
它会起作用