問題描述
我對 Android 中的 EditText 函數有幾個疑問.
I have a couple of queries regarding the EditText function in Android.
首先,是否可以在 EditText 字段中設置最小字符數?我知道有一個
First of all, is it possible to set a minimum number of characters in the EditText field? I'm aware that there is an
android:maxLength="*"
但是由于某種原因你不能擁有
however for some reason you can't have
android:minLength="*"
另外,我想知道是否可以在按下鍵盤上的 Enter 鍵后啟動一個新活動,當我們在 EditText 字段中輸入數據時彈出我們?如果是這樣,有人可以告訴我怎么做嗎?
Also, I was wondering if it is possible to launch a new activity after pressing the enter key on the keyboard that pops us when inputing data into the EditText field? And if so, could someone show me how?
感謝您就這兩個問題提供的任何幫助:)
Thanks for any help you could offer regarding either question :)
推薦答案
響應編輯字段中的回車鍵并在用戶輸入的文本不足時通知用戶:
To respond to an enter key in your edit field and notify the user if they haven't entered enough text:
EditText myEdit = (EditText) findViewById(R.id.myedittext);
myEdit.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
if (myEdit.getText().length() < minLength) {
Toast.makeText(CurrentActivity.this, "Not enough characters", Toast.LENGTH_SHORT);
} else {
startActivity(new Intent(CurrentActivity.this, ActivityToLaunch.class);
}
return true;
}
return false;
}
});
在編輯字段時,沒有簡單的方法來強制設置最小長度.您將檢查輸入的每個字符的長度,然后在用戶嘗試刪除超過最小值時拋出擊鍵.這很混亂,這就是為什么沒有內置的方法來做到這一點.
There's no simple way to force a minimum length as the field is edited. You'd check the length on every character entered and then throw out keystrokes when the user attempt to delete past the minimum. It's pretty messy which is why there's no built-in way to do it.
這篇關于EditText 最小長度 &啟動新活動的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!