2010-06-10 78 views
23

我想使用AlertDialog作为登录或PIN码或密码对话框。这里是我的代码 -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(); 

如何代码,所有的输入文本将出现像“***” - 星号

我不能让我的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

<?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" /> 

我输入的值,但得到空!

如何解决?

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

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

我使用Android 1.6。它依赖于版本吗? :?:

回答

4

EditText应该有android:password="true"

检查this链接。

+0

谢谢你,Macarse。我知道这个android:密码。但我只想使用AlertDialog作为登录表单。这样使用很好吗?我的应用程序有主选项卡布局,然后会出现相关的活动。它看起来像内置联系人。我想在用户开始使用我的应用程序时出现此登录布局。我只知道onResume()事件,但如果我使用此事件,有时登录布局会出现两次或更多次。那么如何在每次用户开始使用应用程序时出现代码。 – soclose 2010-06-10 04:34:31

+0

它工作。我使用LayoutInflater,基于这个链接 - http://www.coderanch.com/t/434894/Android/Mobile/values – soclose 2010-06-10 07:29:55

+0

虽然它显示为星号,但我无法获取我的PIN码值。我在我的帖子中发布了我的更新代码。请检查我的遗漏或错误的一点。 – soclose 2010-06-15 03:52:11

6

此问题可以在不使用弃用android:password的情况下解决。看到我的回答here

-1

替换:

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

有了:

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

什么,....../??????? / – 2013-07-17 11:00:32