Android 如何以编程方式将输入类型设置为EditText的密码
我正在使用带有Android 如何以编程方式将输入类型设置为EditText的密码,android,android-edittext,Android,Android Edittext,我正在使用带有EditText的警报对话框。我想将输入类型设置为以编程方式编辑文本的密码。我发现有两种方法: final EditText input = new EditText(getActivity()); input.setTransformationMethod(PasswordTransformationMethod.getInstance()); input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEX
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();
}
我做错了什么?删除此项并重试
InputType.TYPE\u CLASS\u TEXT
仅尝试此input.setInputType(InputType.TYPE\u TEXT\u变体\u密码)代码>您需要呼叫:
input.setTransformationMethod(PasswordTransformationMethod.getInstance());
如上所述
此外,正如问题中提到的评论:
您需要调用setTransformationMethod而不是setInputType。在setTransformationMethod之后调用setInputType会导致EditText不再处于密码模式
因此,根据我的理解,它应该如下所示:
input.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD );
input.setTransformationMethod(PasswordTransformationMethod.getInstance());
您遇到此问题是因为您正在使用alert.setCustomTitle(title)代码>
之后
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
input.setTransformationMethod(PasswordTransformationMethod.getInstance());
使其再次成为正常类型
任何一项更改:警报。设置自定义标题(标题)
至alert.setTitle(“此处为您的标题”)代码>
或如果您想使用定制
然后使用以下代码
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_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
input.setTransformationMethod(PasswordTransformationMethod.getInstance());
alert.setView(input);
alert.setCustomTitle(title);
你试过了吗?是的,我试过了,但它对我不起作用。在提供的链接中有一条特别的评论:为了补充这一点,你需要调用setTransformationMethod而不是setInputType。在setTransformationMethod之后调用setInputType会导致EditText不再处于密码模式。谢谢,我只尝试了一种方法(第一种或第二种)以及两种方法。但是没有用谢谢你的回答,这也不起作用我试过了,也不起作用了我也试过了-而且效果很好(例如在4.2设备上)。是的,我也试过4.2(平板电脑)。我使用的是碎片,这有什么问题吗fragments@venkataramana看我更新的答案!是的,我看过了,它是一样的。是的,我能打字和看到文字also@venkataramana看我更新的答案+1因为这里是