我有一个EditText,让我输入一个IP地址: 在xml中:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="@+id/enterIP"
android:layout_weight="1"
android:onClick="enterIP"
android:inputType="textPhonetic"
android:imeOptions="actionDone"
/>
我使用了android:imeOptions =&#34; actionDone&#34;按下完成后,输入框将消失。 在Java中:
public void enterIP(View view) {
EditText theIP = (EditText) findViewById(R.id.enterIP);
try {
myIP = theIP.getText().toString();
validIP = ipvalidator.validate(myIP);
} catch (NullPointerException e)
{
Log.d("Error", "Input address is NULL.");
}
Toast.makeText(getApplicationContext(), "New IP is " + myIP, Toast.LENGTH_LONG).show();
}
然而,问题是当我按下Done时,myIP仍保留旧值。只有当我触摸EditText再次调出输入时,才会更新该值。
那么我怎样才能确保在按下Done时更新myIP。 ?
由于
答案 0 :(得分:3)
试试这个
yourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//do something
myIP = theIP.getText().toString();
validIP = ipvalidator.validate(myIP);
}
return false;
}
});
答案 1 :(得分:1)
使用此方法:
theIP.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Do whatever you want here
return true;
}
return false;
}
答案 2 :(得分:0)
你应该为EditText设置onEditorActionListener来实现你想要执行的动作。
java.lang.IllegalStateException: No WebApplicationContext found: not in a DispatcherServlet request and no ContextLoaderListener registered?