编辑文本第一次输入一个字母验证 [英] Edit text first time to input a letter validation
问题描述
我创建如果用户输入一个字母或数字,然后一个按钮,将弹出来检查应用程序。如果用户没有输入一个字母一个验证将显示。
I'm creating an application that checks if the user inputs a letter or a number then a button will pop-out. And if the user didn't enter a single letter a validation will show.
这里是我想要调试[主要活动的例子。 PS。不是我真正的code]:
Here's an example of what I'm trying to debug[main activity. PS. not my real code]:
private EditText edittext;
Button checkBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addKeyListener();
}
public void addKeyListener() {
edittext = (EditText) findViewById(R.id.editText);
checkBtn = (Button) findViewById(R.id.button1);
checkBtn.setVisibility(View.INVISIBLE);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
checkBtn.setVisibility(View.VISIBLE);
return true;
else if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_9)) {
// display a floating message
Toast.makeText(MyAndroidAppActivity.this,
"Number 9 is pressed!", Toast.LENGTH_LONG).show();
return true;
}
return false;
}
});
}
}
问题:
有没有一种方法可以自动使按钮显示当用户键入不点击回车键的信。如果用户没有输入一个字母一个验证会弹出说无效,该按钮将不会显示。
Question: Is there a way to make the button show automatically when a user types a letter without clicking the enter key. And if the user didn't input a letter a validation will pop out saying invalid and the button will not display.
我试图寻找正确的答案,但仍然我找不到它。
I tried looking for the right answer but still I couldn't find it.
推荐答案
您,我的朋友,需要
EditText editText = (EditText)findViewById(R.id.edittext);
editText .addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
; //Do nothing
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
; //Do nothing
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//TODO put your code here.
}
});
现在,只是检查,看看是否能S字符序列中包含了你的信,你会用自己的方式。
Now, just check to see if that "s" Char Sequence contains your letter and you'll be on your way.
编辑:
一旦你的CharSequence的,只是请检查的第一个字符是字母。
Once you have that CharSequence, just check to see that the first char is a letter.
要得到第一个字符,执行
To get the first char, do
s.charAt(0); //Note: if there is nothing here, it could throw a null pointer.
Character类具有您可以用它来看看它是否是一个字母(或数字,大写,小写字母,数字,数字或字母等),称为静态函数
The Character class has a static function that you can use to see if it is a letter (or a number, capital, lowercase, digit, digit or letter, etc) called
Character.isLetter(char);
所以你可以不喜欢
so you could do something like
public void onTextChanged(CharSequence s, int start, int before, int count) {
//Check to see that there is at least 1 char to look at.
//Then check to see if it is a letter.
//(Note: the && is AND, so both things have to be true)
if (s.length() > 0 && Character.isLetter(s.charAt(0))) {
; //Display your button
} else {
; //Display your error
}
}
帮助吗?
您可以创建一个名为MyTextWatcher新类(无论是在它自己的文件,你可以在任何地方或一个类(如果所有编辑的文本都在同一区域)内的类导入它),然后里面做常规的东西,你现在是在专家:
You can create a new class called MyTextWatcher (either in it's own file where you can import it anywhere or as a class within a class (if all of the edit texts are in the same area)), and then inside there do the regular stuff you are now expert at:
public class MyTextWatcher implements TextWatcher {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
}
我个人preFER做它包含的EditText类里面,但它可以在一个单独的文件中去(即导入),然后是更多的重用。而如果因为任何原因,你需要用TextWatcher访问的东西,你可以将它们作为一个构造函数。做类里面的东西的好处是,你没有的东西传递到构造函数。这只是为你做的事情少。但这里是一个构造函数的例子...
I personally prefer doing it inside the class that contains the edittext, but it can go in a separate file (that you import) and then it's more reusable. And if for any reason, you need to access things with your TextWatcher, you can pass them in as a constructor. The advantage of doing things inside the class is that you don't have to pass things into a constructor. It's just less things for you to do. But here is an example of that constructor...
public class MyTextWatcher implements TextWatcher {
private Thing1 mThing1;
private Thing2 mThing2;
public MyTextWatcher(Thing1 t1, Thing2 t2) {
mThing1 = t1;
mThing2 = t2;
}
...
}
请注意:做时只是要小心不要相互连接的东西太多了。它可能会很麻烦以后。 :)
Note: Just be careful not to interlink things too much when doing that. It can get hairy later on. :)
这篇关于编辑文本第一次输入一个字母验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!