【发布时间】:2015-08-01 09:28:27
【问题描述】:
首先,我在论坛上搜索了“如何从其他布局的edittext中获取文本”,并尝试了很多其他方法,然后我决定使用LayoutInflater 但它没有帮助。
LayoutInflater 的实现(我遵循这个指南)
LayoutInflater factory = getLayoutInflater();
View regisText = factory.inflate(R.layout.regis, null);
EditText user = (EditText) regisText.findViewById(R.id.edt1);
String usr = user.getText().toString();
我的父布局名为 activity_input.xml 有 3 个按钮(我没有设置文本按钮)。我只放了 按钮 ID 以便于查看。
<Button
android:id="@+id/btn1">
</Button>
<Button
android:id="@+id/btn2">
</Button>
<Button
android:id="@+id/btn3">
</Button>
点击按钮后,会出现一个对话框选择值(1、2 或 3)来在 activity_input.xml /strong>。
final Dialog dialog1 = new Dialog(user_input.this);
dialog1.setTitle("Select a Number");
dialog1.setContentView(R.layout.game_dialog);
dialog1.show();
button1 = (Button) dialog1.findViewById(R.id.btn1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Button p1_btn = (Button) findViewById(x);
p1_btn.setText("1");
dialog1.dismiss();
}
});
onClick 完美运行。
但我的问题是我想再次在父布局中获取按钮的文本,我尝试实现 Layoutinflater 但它不起作用,这是代码。
LayoutInflater f1 = getLayoutInflater();
View v1 = f1.inflate(R.layout.activity_input, null);
Button btn1 = (Button) v1.findViewById(R.id.b1);
String t1 = btn1.getText().toString();
我检查以确保 String t1 通过打印字符串成功检索,但它没有显示任何内容
希望各位指导尽快帮帮我!
非常感谢。
【问题讨论】:
-
您可以创建一个
static变量并将结果存储在那里,然后从您的dialog访问它 -
你能说得清楚一点吗?在该函数中在哪里声明静态变量?
-
从哪里获得父布局中的按钮文本?当您尝试访问它时,可能尚未设置文本
-
我可以确保在diablog调用后,父布局中的按钮文本设置成功,并立即显示。
标签: android android-layout android-relativelayout