Android ime选项不起作用

时间:2016-07-16 21:48:50

标签: android android-edittext imeoptions

我正在尝试为android ime选项设置一个监听器,它将EditText的值存储在共享首选项中。我把它设置成这样,但是当我按下"返回"键盘上的键,没有任何反应,它永远不会进入听众。关于我为什么做错的任何想法?

@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"/>

3 个答案:

答案 0 :(得分:2)

我认为它不起作用,因为gem install rails -v 4.1.8应该定义到setOnEditorActionListener(而不是你的按钮)。

EditText

如果您在点击mTemperature = (EditText) findViewById(R.id.temperature); mTemperature.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; } }); 后需要采取任何措施,最好将mDone设置为ClickListener

答案 1 :(得分:1)

我怀疑你需要做mTemperature.setOnEditorActionListener,因为它是EditText。另外,检查事件是否为null,当事件为空时,actionId可以等于EditorInfo.IME_ACTION_DONE。

答案 2 :(得分:0)

imeOptions不适用于多行EditText。

将其更改为单行。

在xml文件中设置以下参数:

android:singleLine =“ true”