2

I am trying to set up a listener for the android ime options, that stores the value of the EditText in the shared preferences. I have it set up like so, but when I press the "return" key on my keyboard, nothing happens and it never goes into the listener. Any ideas on why I'm doing wrong?

@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;
        }
    });
}

This is how I set up my 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"/>
4

3 回答 3

3

我相信它不起作用,因为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,最好将 a 设置ClickListenermDone

于 2016-07-16T22:02:31.657 回答
1

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

于 2016-07-16T22:04:55.700 回答
0

imeOptions 不适用于多行 EditText。

将其更改为单行。

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

机器人:单行=“真”

于 2019-12-16T02:29:10.043 回答