gpt4 book ai didi

Android ime 选项不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 08:33:42 27 4
gpt4 key购买 nike

我正在尝试为 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"/>

最佳答案

我认为它不起作用,因为 setOnEditorActionListener 应该定义到您的 EditText(而不是您的 Button)。

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点击后需要做任何 Action ,最好设置一个ClickListenermDone

关于Android ime 选项不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38415963/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com