AlertDialog Input Text

2019-01-23 12:27发布

I'd like to use AlertDialog as a Login or pin code or password dialog. Here is my code -

    AlertDialog.Builder alert = new AlertDialog.Builder(this);                 
alert.setTitle("Login");  
alert.setMessage("Enter Pin :");                

 // Set an EditText view to get user input   
 final EditText input = new EditText(this); 
 alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
    public void onClick(DialogInterface dialog, int whichButton) {  
        String value = input.getText().toString();
        Log.d( TAG, "Pin Value : " + value);
        return;                  
       }  
     });  

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            return;   
        }
    });
            alert.show();

How to code that all input text will appear like ' *** ' - asterisk

I can't get my pin code value although it shows into asterisk. my code is below

    private void accessPinCode_2()
{
    LayoutInflater factory = LayoutInflater.from(this);
    final View textEntryView = factory.inflate(R.layout.dialog_login, null);
    AlertDialog.Builder alert = new AlertDialog.Builder(this);                 
    alert.setTitle("Login");  
 alert.setMessage("Enter Pin :");                
 alert.setView(textEntryView);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
    public void onClick(DialogInterface dialog, int whichButton) {  
        //String value = input.getText().toString();
        EditText mUserText;
        mUserText = (EditText) textEntryView.findViewById(R.id.txt_password);
        String strPinCode = mUserText.getText().toString();
        Log.d( TAG, "Pin Value : " + strPinCode);
        return;                  
       }  
     });  

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            return;   
        }
    });
            alert.show();   }}

dialog_login.xml

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/txt_password"
         android:password="true"
         android:layout_height="wrap_content"
         android:layout_width="250px"
         android:layout_centerHorizontal="true"
         android:layout_below="@+id/password_text"
         android:singleLine="true" /> 

I entered the value, but get null!

how to solve?

Debugging is stopped at this statement. When I pointed above mUserText null value being shown in popup.

String strPinCode = mUserText.getText().toString();

I use Android 1.6. Does it depend on Version? :?:

3条回答
一纸荒年 Trace。
2楼-- · 2019-01-23 12:35

This problem can be solved without using deprecated android:password. See my answer here.

查看更多
\"骚年 ilove
3楼-- · 2019-01-23 12:45

You EditText should have android:password="true".

Check this link.

查看更多
\"骚年 ilove
4楼-- · 2019-01-23 12:49

Replace:

EditText rate = (EditText)findViewById((R.id.rate));

With:

EditText rate = (EditText)wind.findViewById((R.id.rate));
查看更多
登录 后发表回答