如何设置inputtypes为密码编辑文本编辑

大家好,我正在使用编辑文本的alert对话框。所以我想设置inputtypes作为密码编辑文本编程方式。我在谷歌search了很多,找出这两个方法:

final EditText input = new EditText(getActivity()); input.setTransformationMethod(PasswordTransformationMethod.getInstance()); input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 

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

 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

Solutions Collecting From Web of "如何设置inputtypes为密码编辑文本编辑"

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

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

这使它再次正常types

或者更改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); 

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

删除这个并尝试

InputType.TYPE_CLASS_TEXT

你需要打电话给:

 input.setTransformationMethod(PasswordTransformationMethod.getInstance()); 

如此处所述。

另外,正如评论提到的问题:

你需要调用setTransformationMethod而不是setInputType 。 在setTransformationMethod之后调用setInputType将导致EditText不再处于密码模式。

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

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