如何以编程方式将输入类型设置为编辑文本的密码

时间:2013-07-16 10:57:31

标签: android android-edittext

大家好我正在使用编辑文本的警告对话框。所以我想以编程方式将输入类型设置为编辑文本的密码。我在谷歌搜索过很多并找出这两种方法:

final EditText input = new EditText(getActivity()); 

input.setTransformationMethod(PasswordTransformationMethod.getInstance());

input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 

但它不适合我,它显示文字。但我想要点缀文字。我不知道的问题是什么。所以请建议我做。 感谢所有提前。 这是带编辑文本的对话框代码:

public void showDialog(){

           /* Alert Dialog Code Start*/     
                AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
//              alert.setTitle("JPOP"); //Set Alert dialog title here
                alert.setMessage("              Please enter password"); //Message here

                Log.e("dialog in password ","passworddddddddddddddddd");

                // Set an EditText view to get user input 
                final EditText input = new EditText(getActivity());
//              input.setInputType(InputType.TYPE_CLASS_TEXT);
//              input.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
//              input.setTransformationMethod(PasswordTransformationMethod.getInstance());

//              final EditText input = new EditText(getActivity()); 
                input.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
                input.setTransformationMethod(new PasswordTransformationMethod());


                input.setHint("Password");
                input.setSingleLine();
                input.setTextSize(14);
                alert.setView(input);

                alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    strPassword = input.getEditableText().toString().trim();    

                    if(strPassword.length()!=0){

                 String prestatus =DataUrls.preferences.getString("Password", "");
                 if(prestatus.equals(strPassword)){

                     if(price_reports_check){

                         price_reports_check=false;

                            ReportsFragment reportfragment = new ReportsFragment();
                            FragmentManager fragmentManager = getFragmentManager();
                            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                            fragmentTransaction.replace(R.id.details, reportfragment);
                            fragmentTransaction.commit();   
                     }else{
                        PriceListFragment pricelistfragment = new PriceListFragment();
                        FragmentManager fragmentManager = getFragmentManager();
                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        fragmentTransaction.replace(R.id.details, pricelistfragment);
                        fragmentTransaction.commit();
                     }

                 }else
                 {
                     showDialog();
                     Toast.makeText(getActivity(), "The password you entered is wrong", Toast.LENGTH_SHORT).show();
                 }

                    }
                    else
                    {
                        showDialog();
                        Toast.makeText(getActivity(), "Please Enter Password", Toast.LENGTH_SHORT).show();

                    }

                } // End of onClick(DialogInterface dialog, int whichButton)
            }); //End of ok....

                alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int whichButton) {
                    // Canceled.
                      dialog.cancel();
                  }
            }); //End of alert.setNegativeButton


                AlertDialog alertDialog = alert.create();

                   TextView title = new TextView(getActivity());
                  // You Can Customise your Title here 
                    title.setText("JPOP");
//                  title.setBackgroundColor(Color.DKGRAY);
                    title.setPadding(10, 10, 10, 10);
                    title.setGravity(Gravity.CENTER);
//                  title.setTextColor(Color.WHITE);
                    title.setTextSize(20);
                    alert.setCustomTitle(title);
                    alert.setCancelable(false);

                    alert.show();


         }

所以请帮助我做错了。谢谢@All

4 个答案:

答案 0 :(得分:11)

您遇到此问题因为您正在使用 alert.setCustomTitle(title);

<强>后

input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        input.setTransformationMethod(PasswordTransformationMethod.getInstance());

使再次成为正常类型

要么改变alert.setCustomTitle(title); 改为 alert.setTitle("your title here");

,如果您想要使用 customeTitle

使用以下代码

input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
input.setTransformationMethod(PasswordTransformationMethod.getInstance());
alert.setView(input);

之后
alert.setCustomTitle(title);

答案 1 :(得分:3)

仅尝试此input.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

答案 2 :(得分:0)

删除此内容并尝试

InputType.TYPE_CLASS_TEXT

答案 3 :(得分:-1)

您需要致电:

input.setTransformationMethod(PasswordTransformationMethod.getInstance());

如上所述here

此外,正如评论中提到的那样:

您需要调用 setTransformationMethod而不是setInputType 。在setTransformationMethod之后调用setInputType会导致EditText再次无法进入密码模式。

因此,根据我的理解,它应该如下所示:

    input.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD );
    input.setTransformationMethod(PasswordTransformationMethod.getInstance());