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.
它似乎没有任何影响。
9 个解决方案
#1
192
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.
应该做的伎俩。
#2
365
For setting the input type for an EditText programmatically, you have to specify that input class type is text.
要以编程方式设置EditText的输入类型,必须指定输入类类型为text。
editPass.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
#3
25
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)
看点(如果数据是数字,则不是选择其他类)
#4
13
To only allow numbers:
仅允许数字:
password1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_CLASS_NUMBER);
To transform (hide) the password:
要转换(隐藏)密码:
password1.setTransformationMethod(PasswordTransformationMethod.getInstance());
#5
8
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
<EditText
android:inputType="text"
/>
Other options besides text
can be found in the documentation.
除文本外的其他选项可在文档中找到。
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_TEXT_FLAG_IME_MULTI_LINE));
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
- Getting the current
InputType
获取当前的InputType
#6
7
editText.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
//you can change TYPE_... according to your requirement.
//你可以根据你的要求改变TYPE _...
#7
2
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 this for the solution. Wish I had found that a few hours ago!
我必须相信这个解决方案。希望我几小时前找到了!
#8
0
I know the expected Answer is in Java
. But here's my 2 cents of advice always try to handle view related stuff in XML
(atleast basic stuff) so I would suggest rather use a xml
attribute rather than handling this use case in java
我知道预期的答案是Java。但是这里我的2美分建议总是试图处理XML中的视图相关内容(至少基本的东西)所以我建议使用xml属性而不是在java中处理这个用例
<EditText
android:inputType="textPassword"/>
#9
-1
Try adding this to the EditText/TextView tag in your layout
尝试将其添加到布局中的EditText / TextView标记
android:password="true"
Edit: I just re-read your post, perhaps you need to do this after construction. I don't see why your snippet wouldn't work.
编辑:我刚刚重新阅读你的帖子,也许你需要在施工后这样做。我不明白为什么你的代码片不起作用。
#1
192
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.
应该做的伎俩。
#2
365
For setting the input type for an EditText programmatically, you have to specify that input class type is text.
要以编程方式设置EditText的输入类型,必须指定输入类类型为text。
editPass.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
#3
25
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)
看点(如果数据是数字,则不是选择其他类)
#4
13
To only allow numbers:
仅允许数字:
password1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_CLASS_NUMBER);
To transform (hide) the password:
要转换(隐藏)密码:
password1.setTransformationMethod(PasswordTransformationMethod.getInstance());
#5
8
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
<EditText
android:inputType="text"
/>
Other options besides text
can be found in the documentation.
除文本外的其他选项可在文档中找到。
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_TEXT_FLAG_IME_MULTI_LINE));
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
- Getting the current
InputType
获取当前的InputType
#6
7
editText.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
//you can change TYPE_... according to your requirement.
//你可以根据你的要求改变TYPE _...
#7
2
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 this for the solution. Wish I had found that a few hours ago!
我必须相信这个解决方案。希望我几小时前找到了!
#8
0
I know the expected Answer is in Java
. But here's my 2 cents of advice always try to handle view related stuff in XML
(atleast basic stuff) so I would suggest rather use a xml
attribute rather than handling this use case in java
我知道预期的答案是Java。但是这里我的2美分建议总是试图处理XML中的视图相关内容(至少基本的东西)所以我建议使用xml属性而不是在java中处理这个用例
<EditText
android:inputType="textPassword"/>
#9
-1
Try adding this to the EditText/TextView tag in your layout
尝试将其添加到布局中的EditText / TextView标记
android:password="true"
Edit: I just re-read your post, perhaps you need to do this after construction. I don't see why your snippet wouldn't work.
编辑:我刚刚重新阅读你的帖子,也许你需要在施工后这样做。我不明白为什么你的代码片不起作用。