简体   繁体   中英

How can I see the text that I'm typing in editText window in alertDialog?

I have the following code for an AlertDialog window in which I want the user to input a number(that I'm storing in int m_Text ). I have 2 problems: I can't see the numbers that I'm typing and if I press enter without any numbers it will crash. How can I solve them ? (the method pressMe() is executed when I press a button)

public void pressMe(){
       final EditText input = new EditText(this);
       input.setInputType(InputType.TYPE_CLASS_NUMBER );
        AlertDialog.Builder myAlert = new AlertDialog.Builder(this);
        myAlert.setMessage("Enter number:")
                .setView(input)
                .setPositiveButton("Enter", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        m_Text = Integer.parseInt(input.getText().toString());
                        Log.d(TAG," Number :  "+m_Text");
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .setTitle("Test")
                .setIcon(R.drawable.ic_launcher)
                .create();
        myAlert.show();
    }

The answer why your application is crashed is here:

Integer.parseInt(input.getText().toString());

When input text is empty you try to parse "" on Integer and you get NumberFormatException .

you have to handle this situation for example like in code below:

final String str = input.getText().toString().trim();
m_Text = str.length() == 0 ? 0 : Integer.parseInt(str);

or

final String str = input.getText().toString().trim();
if(str.length() != 0){
     m_Text = Integer.parseInt(str);
}

I don't understand your first problem. If you explain it to me, I help you.

Update When I tried your code I have result like below:

图片

Read this document http://developer.android.com/guide/topics/ui/dialogs.html

if you want to use any types of dialog in your application.

1)Create a class that will extend DialogFragment class.

2)Overide onCreateDialog() method and inside that write your code for both Positive button and negative button

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage(R.string.dialog_fire_missiles)
           .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // FIRE ZE MISSILES!
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // User cancelled the dialog
               }
           });
    // Create the AlertDialog object and return it
    return builder.create();
}

}

3)Call that Fragment from HostActivity using this code

DialogFragment newFragment = new YourFragmentClass();
newFragment.show(getSupportFragmentManager(), "missiles");

if you want a CustomLayout then

1)create a XML file in res->customLayout.xml and inside that place the view you want(In your case draw a Single EditText on that layout),do not add Button for Positive or Negative Button they are already add by DialogFragment.

2)Inside onCreateDialog() of dialogFragment do something like this

// Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    final EditText et_text=(EditText)getActivity().findViewById(R.id.editTextId);

    builder.setView(inflater.inflate(R.layout.custom_layout, null))

           // Add action buttons
           .setPositiveButton(R.string.send, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {


                   String mob=et_text.getText().toString();

                    //Do What you want to do with EditText

           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {

                   //Coding for Negative Button Here
               }
           });      
    return builder.create();

}

you can Pass EventBack to Hosting Activity. Read reference document

Hope this will help you

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