Mat*_*att 72 android android-edittext
如何知道我的编辑文本何时完成编辑?就像用户选择下一个框,或按下软键盘上的完成按钮一样.
我想知道这个,所以我可以输入.在输入每个字符后,看起来像文本观察者的afterTextChanged.我需要对输入进行一些计算,所以我想在输入每个字符后避免进行计算.
谢谢
Ren*_*eno 104
通过使用这样的东西
meditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
switch (actionId){
case EditorInfo.IME_ACTION_DONE:
case EditorInfo.IME_ACTION_NEXT:
case EditorInfo.IME_ACTION_PREVIOUS:
yourcalc();
return true;
}
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
Ric*_*ich 42
EditText
继承setOnFocusChangeListener
哪个实现OnFocusChangeListener
.
实现onFocusChange
并且有一个布尔参数hasFocus
.当这是错误的时候,你已经失去了对另一个控件的关注.
编辑
要处理这两种情况 - 编辑失去焦点的文本或用户单击"完成"按钮 - 创建一个从两个侦听器调用的方法.
private void calculate() { ... }
btnDone.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
calculate();
}
});
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus)
calculate();
}
});
Run Code Online (Sandbox Code Playgroud)
jsa*_*arb 14
使用如下定义的EditText对象xml:
<EditText
android:id="@+id/create_survey_newquestion_editText_minvalue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:ems="4"
android:imeOptions="actionDone"
android:inputType="number" />
Run Code Online (Sandbox Code Playgroud)
当用户单击软键盘上的完成按钮(OnEditorActionListener)或ii)当EditText丢失了用户焦点(OnFocusChangeListener)时,我们可以捕获其文本i),该焦点现在位于另一个EditText上:
/**
* 3. Set the min value EditText listener
*/
editText= (EditText) this.viewGroup.findViewById(R.id.create_survey_newquestion_editText_minvalue);
editText.setOnEditorActionListener(new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
String input;
if(actionId == EditorInfo.IME_ACTION_DONE)
{
input= v.getText().toString();
MyActivity.calculate(input);
return true; // consume.
}
return false; // pass on to other listeners.
}
});
editText.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
String input;
EditText editText;
if(!hasFocus)
{
editText= (EditText) v;
input= editText.getText().toString();
MyActivity.calculate(input);
}
}
});
Run Code Online (Sandbox Code Playgroud)
这适合我.使用如下代码进行计算后,您可以隐藏软键盘:
private void hideKeyboard(EditText editText)
{
InputMethodManager imm= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
Run Code Online (Sandbox Code Playgroud)
编辑:向onEditorAction添加返回值
我做了这个简单的事情,当焦点从编辑文本转移时获取文本。如果用户将焦点转移到选择其他视图(例如按钮或其他 EditText 或任何视图),这将起作用。
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
EditText editText = (EditText) v;
String text = editText.getText().toString();
}
}
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
94378 次 |
最近记录: |