【发布时间】:2016-11-19 20:03:39
【问题描述】:
我正在尝试为 android ime 选项设置一个侦听器,它将EditText 的值存储在共享首选项中。我已经这样设置了,但是当我按下键盘上的“return”键时,什么也没有发生,它永远不会进入听众。关于我为什么做错的任何想法?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
mDone = (Button) findViewById(R.id.done);
mTemperature = (EditText) findViewById(R.id.temperature);
mDone.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
int action = event.getAction();
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Do whatever you want here
SharedPreferences preferences = getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor edit = preferences.edit();
int updateSweater = Integer.parseInt(mTemperature.getText().toString());
edit.remove("sweater");
edit.putInt("sweater", updateSweater);
edit.commit();
preferences.getInt("sweater", 0);
Toast.makeText(SettingsActivity.this, "Sweater Weather Updated", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
startActivity(intent);
return true;
}
return false;
}
});
}
这就是我设置 EditText 的方式
<EditText
android:id="@+id/temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textColorHint="@color/gray"
android:textSize="25sp"
android:layout_centerHorizontal="true"
android:hint="Farenheit"
android:inputType="phone"
android:imeOptions="actionDone"/>
【问题讨论】:
-
你是如何定义 mDone 的?
-
@GuilhermeP 我为上下文添加了更多代码
标签: android android-edittext imeoptions