本文介绍了如何以编程方式将输入类型设置为 EditText 的密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
限时送ChatGPT账号..我正在使用带有 EditText
的警报对话框.我想以编程方式将输入类型设置为该编辑文本的密码.我找到了这两种方法:
I am using alert dialog with EditText
. I want to set input type as password to that edit text programmatically. I found these two methods:
final EditText input = new EditText(getActivity());
input.setTransformationMethod(PasswordTransformationMethod.getInstance());
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
但是,这些对我不起作用.它显示文本,但我想要 EditText
.
However, these don't work for me. It shows the text, but I want EditText
.
这是带有EditText
的对话框代码:
This is the code of dialog with 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);
fragmentTransactionmit();
}else{
PriceListFragment pricelistfragment = new PriceListFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.details, pricelistfragment);
fragmentTransactionmit();
}
}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();
}
我做错了什么?
推荐答案
你遇到这个问题因为你正在使用 alert.setCustomTitle(title);
之后
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
input.setTransformationMethod(PasswordTransformationMethod.getInstance());
这使得它再次成为普通类型
要么改变:alert.setCustomTitle(title);
to 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);
这篇关于如何以编程方式将输入类型设置为 EditText 的密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论