【问题标题】:Android EditText doesn't process Keyboard InputAndroid EditText 不处理键盘输入
【发布时间】: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


【解决方案1】:

试试这个代码:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setTitle("Your Title");

    LayoutInflater inflater = getActivity().getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.addplayer_fragment, null);
    builder.setView(dialogView)

    .setPositiveButton("Save", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {

                EditText valueView = (EditText) dialogView.findViewById(R.id.playerNameEdit); 
                if(valueView == null) Log.d("VIEW", "NULL");
                else{
                    String playerName = playerNameEdit.getText().toString();
                    Toast.makeText(getActivity(),playerName , Toast.LENGTH_SHORT).show();
                }
            })

    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {

        }
    }); 

    return builder.create();

}

【讨论】:

  • 感谢您的解决方案给了我一个重要提示:我没有对发送到构建器视图的布局进行膨胀。
【解决方案2】:

你也可以在fragment里面使用下面的方法

void showDialog()
{
    View view=getActivity().getLayoutInflater().inflate(R.layout.addplayer_fragment,null);
    final EditText playerNameEdit = (EditText)view.findViewById(R.id.playerNameEdit);
    new AlertDialog.Builder(getActivity())
            .setTitle("Title here")
            .setMessage("Message here")
            .setView(view)
            .setPositiveButton("Save", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    String name=playerNameEdit.getText().toString();
                    Toast.makeText(getActivity(), name, Toast.LENGTH_SHORT).show();
                }
            })
            .create().show();
}

【讨论】:

    猜你喜欢
    • 2011-07-12
    • 1970-01-01
    • 2023-03-09
    • 2014-05-28
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    相关资源
    最近更新 更多