Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

android - AlertDialog Input Text

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? :?:

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...