Android 警报对话框输入文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3011361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:16:39  来源:igfitidea点击:

AlertDialog Input Text

androidandroid-alertdialog

提问by soclose

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

我想使用 AlertDialog 作为登录或密码或密码对话框。这是我的代码 -

    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

我无法获得我的 PIN 码值,尽管它显示为星号。我的代码在下面

    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

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.

调试在此语句处停止。当我指向弹出窗口中显示的 mUserText 空值时。

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

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

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

我使用安卓 1.6。它取决于版本吗?:?:

采纳答案by Macarse

You EditTextshould have android:password="true".

EditText应该有android:password="true"

Check thislink.

检查链接。

回答by Viktor Bre?an

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

这个问题可以在不使用 deprecated 的情况下解决android:password在这里看到我的答案。

回答by Jagjit Warwal

Replace:

代替:

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

With:

和:

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