简体   繁体   中英

Android EditText doesn't process Keyboard Input

I have a DialogFragment with a simple custom layout (TextView and EditView).

When the user clicks the Positive Button of the Dialog, the app should save the user input from the EditText field into the variable "playerName". No matter what i'm trying, the Toast-output always shows an empty string "", or rather when i hardcode a text into the EditText (as shown in the XML) it shows always that text.

The changes the user does with the keyboard input don't get processed in the code, any ideas what's wrong?

DialogFragment

@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();
        }

Layout (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>

You can also use the following method inside the 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();
}

Try this code:

@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();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM