【发布时间】:2016-03-09 23:42:31
【问题描述】:
我在这里发现了一些类似的问题,但是对于像我这样的新手来说,所有这些问题要么真的很困惑,要么无助于解决我的问题: 我有一个 TextView 电视。通过单击它,我想显示一个带有 EditText + Cancel- 和 Update-Buttons 的自定义 AlertDialog。通过单击我的对话框的更新按钮,我想用对话框中的 EditText 的值替换相同 TextView (tv) 的文本。这是我的尝试之一:
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv̶̶C̶̶l̶̶i̶̶c̶̶k̶̶e̶̶d̶ = (TextView) findViewById(R.id.tv_id);
tv.setText("Initial value");
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
UpdateDialogFragment updDiag = new UpdateDialogFragment();
updDiag.show(getFragmentManager(), "dialog");
tv.setText(updDiag.value); // I try to get the value of EditText like this, but it doesn't work
}
});
}
public class UpdateDialogFragment extends DialogFragment {
String value; // I try to get the value of EditText like this, but it doesn't work
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.edit_tv, null))
.setPositiveButton("Update", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
EditText et = (EditText) UpdateDialogFragment.this.getDialog().findViewById(R.id.et_tv);
value = et.getText().toString(); // I try to get the value of EditText like this, but it doesn't work
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
UpdateDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
}
还有我的edit_tv.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/et_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginTop="40dp"
android:hint="Enter text ..."
android:layout_gravity="center_horizontal">
<requestFocus />
</EditText>
</LinearLayout>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.yev.tabletasting.MainActivity">
<TextView
android:id="@+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
在我的情况下,点击“更新”后,电视的文本将设置为“”(可能为空)。 我会感谢你的每一个建议,在期待中感谢你!
【问题讨论】:
-
whats tvclicked 和 tv?他们是两个文本视图吗?无论如何,您要设置文本的textview,全局声明它..并在
value = et.getText().toString();之后在setPositiveButton onClick中执行textview.settext(value) -
@Dhina 我已经编辑了我的代码,tvClicked 应该只是 tv(错字)。谢谢你的回答,我的问题是正确的,我问它和工作的方式是正确的,但我将来可能会使用动态 textViews,比如 textView tv = new TextView(this);而且不可能在全球范围内声明它
标签: android android-edittext textview android-dialogfragment android-dialog