我在这里发现了一些类似的问题,但他们所有人都对我这样的新手感到困惑,或者没有帮助解决我的问题: 我有一个TextView电视。通过单击它我想显示一个带EditText + Cancel-和Update-Buttons的自定义AlertDialog。通过单击我的对话框的更新按钮,我想用Dialog中的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>
在我的情况下,电视文本将被设置为&#34;&#34; (可能为null)点击&#34;更新&#34;。 我会感谢你的每一条建议,感谢你的期待!
答案 0 :(得分:1)
这是你在TextView onClickListener中应该拥有的:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Write new value for TextView");
final EditText editText = new EditText(this);
builder.setView(editText);
builder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newValue = editText.getText().toString();
if(newValue.lenght() == 0)
Toast.makeText(this, "You need to type something in the editText", Toast.LENGTH_LONG).show();
}else{
tv.setText(newValue);
dialog.dismiss();
}
});
可能需要进行一些小的更改,因为我在这里编写了代码。希望能帮助到你。祝你好运:)
LE:对于editText对话框,您不需要额外的Fragment和xml文件。
答案 1 :(得分:0)
好的,所以你还没有真正提到过为什么你喜欢使用DialogFragment
而不仅仅是AlertDialog
,正如Victor Holotescu所回答的那样。但是接下来,当我尝试点击更新按钮时,我尝试了你的代码并设法收到 NullPointerException 。所以我查看了它并修改了代码,这里是:
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv = (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().newInstance(tv); // Passed the TextView here
updDiag.show(getFragmentManager(), "dialog");
}
});
}
public static class UpdateDialogFragment extends DialogFragment {
String value; // I try to get the value of EditText like this, but it doesn't work
TextView tvToEdit;
public UpdateDialogFragment newInstance(TextView tvToEdit){
UpdateDialogFragment udf = new UpdateDialogFragment();
udf.tvToEdit = tvToEdit;
return udf;
}
@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();
tvToEdit.setText(value);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
UpdateDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
}
}
我在代码中添加了一些TODO
条评论(在下面标有阅读评论。:)),检查出来。我测试了它,它运行正常。希望这能够以某种方式帮助你。有关AlertDialog
和DialogFragment
的更多信息,这里有一个很好的帖子 - SendCancelNotification。
修改
好。所以我编辑了它,你只需要通过创建一个newInstance方法将TextView传递给UpdateDialogFragment,在这里引用它DialogFragment advantages over AlertDialog - 希望这会有所帮助。 :)