更新的Android Activity的UI我有一个Activity
其布局是包含TextView
id为my_view
,并与ID open_alert
一个Button
文件main.xml
英寸点击按钮将打开一个AlertDialog
,点击确定即可解散。一旦AlertDialog
被取消,我需要更新活动中TextView
的值。如何从警告对话框
我无法从活动更新TextView
的值。
更新的Android Activity的UI我有一个Activity
其布局是包含TextView
id为my_view
,并与ID open_alert
一个Button
文件main.xml
英寸点击按钮将打开一个AlertDialog
,点击确定即可解散。一旦AlertDialog
被取消,我需要更新活动中TextView
的值。如何从警告对话框
我无法从活动更新TextView
的值。
一种方法是重新启动在OnClickListener的活动在您的快讯。如果你想传递一个值到视图中,只需在intent中添加一个额外的属性,并重置TextView中的值。我已经发布了一个粗略的代码以供参考。
alert.setNeutralButton("OK", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//create a new intent
Intent intent = new Intent("YOUR ACTIVITY NAME");
//add your value in the intent
int value = //your value
intent.putExtra("value",value);
//start your activity
startActivity(intent);
}
});
,并在您的活动
Intent intent = getIntent();
yourTextView.setText(intent.getExtras().getString("value"));
不知道这是否是最有效的方式做到这一点,但应该工作。
它会工作,但我们需要再次完成活动并重新启动它,这不是那个gud的想法。而不是像下面这样点击按钮时更新UI:
按钮b =(按钮)findviewbyId(R.id.b1); –
2012-02-05 16:46:40
只实现onClick
侦听肯定按钮:
new AlertDialog.Builder(this)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
TextView text = (TextView) v.findViewById(R.id.my_view);
if (text != null)
{
text.setText("new text");
}
})
.setNegativeButton(R.string.cancel, null).create().show();
首先,您应该确保android:editable属性存在于XML文件中,并对目标TextView设置为true。然后,在onClick()方法中,您只需要使用setText()。
提供更多详细信息 - 错误日志?代码失败了吗? – BitBank 2012-02-05 15:13:06
提供您已经尝试过的代码... – 2012-02-05 15:15:51