11

我正在使用带有EditText. 我想以编程方式将输入类型设置为该编辑文本的密码。我找到了这两种方法:

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

input.setTransformationMethod(PasswordTransformationMethod.getInstance());

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

但是,这些对我不起作用。它显示文本,但我想要EditText.

这是对话框的代码EditText

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();
           
                
         }

我做错了什么?

4

4 回答 4

12

您遇到此问题是因为您正在使用 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);
于 2013-07-16T11:10:25.717 回答
5

只试试这个input.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

于 2013-07-16T11:06:58.783 回答
0

删除它并尝试

InputType.TYPE_CLASS_TEXT

于 2013-07-16T11:02:47.360 回答
-1

You need to call:

input.setTransformationMethod(PasswordTransformationMethod.getInstance());

As described here.

Also, as comments mentions in provided question:

you need to call setTransformationMethod instead of setInputType. Calling setInputType after setTransformationMethod causes the EditText to not be in password mode again.

So, it should look like the following per my understanding:

    input.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD );
    input.setTransformationMethod(PasswordTransformationMethod.getInstance());
于 2013-07-16T11:07:56.143 回答