Android 以编程方式为 EditText 设置 inputType?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2586301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set inputType for an EditText Programmatically?
提问by user246114
How do we set the input type for an EditText programatically? I'm trying:
我们如何以编程方式设置 EditText 的输入类型?我想:
mEdit.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
it doesn't seem to have any effect.
它似乎没有任何效果。
采纳答案by rascalking
According to the TextView docs, the programmatic version of android:password is setTransformationMethod(), not setInputType(). So something like:
根据TextView 文档,android:password 的编程版本是setTransformationMethod(),而不是 setInputType()。所以像:
mEdit.setTransformationMethod(PasswordTransformationMethod.getInstance());
should do the trick.
应该做的伎俩。
回答by Amitku
For setting the input type for an EditText programmatically, you have to specify that input class type is text.
要以编程方式设置 EditText 的输入类型,您必须指定输入类类型为文本。
editPass.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
回答by Suragch
Here are the various Input Types as shown on the standard keyboard.
以下是标准键盘上显示的各种输入类型。
Setting the input type programmatically
以编程方式设置输入类型
editText.setInputType(InputType.TYPE_CLASS_TEXT);
Other options besides TYPE_CLASS_TEXT
can be found in the documentation.
其他选项TYPE_CLASS_TEXT
可以在文档中找到。
Setting the input type in XML
在 XML 中设置输入类型
<EditText
android:inputType="text"
/>
Other options besides text
can be found in the documentation.
其他选项text
可以在文档中找到。
Supplemental code
补充代码
Here is the code for the image above.
这是上图的代码。
public class MainActivity extends AppCompatActivity {
EditText editText;
TextView textView;
List<InputTypeItem> inputTypes;
int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
textView = findViewById(R.id.textView);
initArray();
}
public void onChangeInputTypeButtonClick(View view) {
if (counter >= inputTypes.size()) {
startOver();
return;
}
textView.setText(inputTypes.get(counter).name);
editText.setInputType(inputTypes.get(counter).value);
editText.setSelection(editText.getText().length());
counter++;
}
private void startOver() {
counter = 0;
textView.setText("");
editText.setInputType(InputType.TYPE_CLASS_TEXT);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
private void initArray() {
inputTypes = new ArrayList<>();
inputTypes.add(new InputTypeItem("date", InputType.TYPE_CLASS_DATETIME | InputType.TYPE_DATETIME_VARIATION_DATE));
inputTypes.add(new InputTypeItem("datetime", InputType.TYPE_CLASS_DATETIME | InputType.TYPE_DATETIME_VARIATION_NORMAL));
inputTypes.add(new InputTypeItem("none", InputType.TYPE_NULL));
inputTypes.add(new InputTypeItem("number", InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL));
inputTypes.add(new InputTypeItem("numberDecimal", InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL));
inputTypes.add(new InputTypeItem("numberPassword", InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD));
inputTypes.add(new InputTypeItem("numberSigned", InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED));
inputTypes.add(new InputTypeItem("phone", InputType.TYPE_CLASS_PHONE));
inputTypes.add(new InputTypeItem("text", InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL));
inputTypes.add(new InputTypeItem("textAutoComplete", InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE));
inputTypes.add(new InputTypeItem("textAutoCorrect", InputType.TYPE_TEXT_FLAG_AUTO_CORRECT));
inputTypes.add(new InputTypeItem("textCapCharacters", InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS));
inputTypes.add(new InputTypeItem("textCapSentences", InputType.TYPE_TEXT_FLAG_CAP_SENTENCES));
inputTypes.add(new InputTypeItem("textCapWords", InputType.TYPE_TEXT_FLAG_CAP_WORDS));
inputTypes.add(new InputTypeItem("textEmailAddress", InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS));
inputTypes.add(new InputTypeItem("textEmailSubject", InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_SUBJECT));
inputTypes.add(new InputTypeItem("textFilter", InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_FILTER));
inputTypes.add(new InputTypeItem("textLongMessage", InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_LONG_MESSAGE));
inputTypes.add(new InputTypeItem("textMultiLine", InputType.TYPE_TEXT_FLAG_MULTI_LINE));
inputTypes.add(new InputTypeItem("textNoSuggestions", InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS));
inputTypes.add(new InputTypeItem("textPassword", InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
inputTypes.add(new InputTypeItem("textPersonName", InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PERSON_NAME));
inputTypes.add(new InputTypeItem("textPhonetic", InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PHONETIC));
inputTypes.add(new InputTypeItem("textPostalAddress", InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS));
inputTypes.add(new InputTypeItem("textShortMessage", InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE));
inputTypes.add(new InputTypeItem("textUri", InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI));
inputTypes.add(new InputTypeItem("textVisiblePassword", InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD));
inputTypes.add(new InputTypeItem("textWebEditText", InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT));
inputTypes.add(new InputTypeItem("textWebEmailAddress", InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS));
inputTypes.add(new InputTypeItem("textWebPassword", InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD));
inputTypes.add(new InputTypeItem("time", InputType.TYPE_CLASS_DATETIME | InputType.TYPE_DATETIME_VARIATION_TIME));
}
class InputTypeItem {
private String name;
private int value;
InputTypeItem(String name, int value) {
this.name = name;
this.value = value;
}
}
}
See also
也可以看看
回答by skinflint
i've solve all with
我已经解决了所有问题
.setInputType(InputType.TYPE_CLASS_NUMBER);
for see clear data and
查看清晰的数据和
.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
for see the dots (if the data is a number, it isn't choice che other class)
查看点(如果数据是数字,则不是其他类的选择)
回答by slinden77
To only allow numbers:
只允许数字:
password1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_CLASS_NUMBER);
To transform (hide) the password:
转换(隐藏)密码:
password1.setTransformationMethod(PasswordTransformationMethod.getInstance());
回答by Ajeet
editText.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
//you can change TYPE_... according to your requirement.
//您可以根据您的要求更改TYPE_...。
回答by Ronen
For Kotlin:
对于科特林:
val password = EditText(this)
password.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
password.hint = "Password"
回答by Brian Reinhold
This may be of help to others like me who wanted to toggle between password and free-text mode. I tried using the input methods suggested but it only worked in one direction. I could go from password to text but then I could not revert. For those trying to handle a toggle (eg a show Password check box) use
这可能对像我这样想要在密码和自由文本模式之间切换的其他人有所帮助。我尝试使用建议的输入法,但它只在一个方向上起作用。我可以从密码到文本,但后来我无法恢复。对于那些试图处理切换(例如显示密码复选框)的人,请使用
@Override
public void onClick(View v)
{
if(check.isChecked())
{
edit.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
Log.i(TAG, "Show password");
}
else
{
edit.setTransformationMethod(PasswordTransformationMethod.getInstance());
Log.i(TAG, "Hide password");
}
}
I have to credit thisfor the solution. Wish I had found that a few hours ago!
我必须将此归功于解决方案。希望我在几个小时前就找到了!
回答by Alif
To unhide password:
取消隐藏密码:
editText.setInputType(
InputType.TYPE_CLASS_TEXT|
InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
);
To hide password again:
再次隐藏密码:
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
回答by miragessee
Hide:
隐藏:
edtPassword.setInputType(InputType.TYPE_CLASS_TEXT);
edtPassword.setTransformationMethod(null);
Show:
展示:
edtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
edtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());