【发布时间】:2017-03-24 13:23:02
【问题描述】:
我有一个带有简单自定义布局(TextView 和 EditView)的 DialogFragment。
当用户单击对话框的正按钮时,应用程序应将用户输入从 EditText 字段保存到变量“playerName”中。无论我尝试什么,Toast 输出总是显示一个空字符串“”,或者更确切地说,当我将文本硬编码到 EditText(如 XML 中所示)时,它总是显示该文本。
用户对键盘输入所做的更改没有在代码中得到处理,有什么问题吗?
对话框片段
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.addplayer_fragment, null);
// declare the text input field
EditText playerNameEdit = (EditText)view.findViewById(R.id.playerNameEdit);
//read text into String
String playerName = playerNameEdit.getText().toString();
// make toast with input of the edit text field
Toast.makeText(getActivity().getApplicationContext(), playerName, Toast.LENGTH_SHORT).show();
}
布局 (addplayer_fragment.xml)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding= "10dip" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name: "/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TestText"
android:id="@+id/playerNameEdit"/>
</LinearLayout>
</RelativeLayout>
【问题讨论】:
-
这是因为在按钮的点击事件中,您会发现新的
EditText而不是现有的对话框,所以您不应该在按钮的点击事件中看到inflate -
感谢您的回答。我已经尝试过在点击事件之外膨胀视图,但仍然是同样的现象
-
HI 创建一个带有 aaccepta dn 拒绝的自定义视图
标签: android input android-edittext