2016-07-16 2 views
2

Я пытаюсь настроить прослушиватель для параметров android ime, который хранит значение EditText в общих настройках. Я настроил его так, но когда я нажимаю клавишу «возврат» на моей клавиатуре, ничего не происходит, и он никогда не входит в слушателя. Любые идеи о том, почему я делаю неправильно?Параметры Android ime не работают

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

Как вы определили mDone? – W0rmH0le

+0

@GuilhermeP Я добавил больше кода для контекста – Rafa

ответ

2

Я считаю, что это не работает, потому что 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 к mDone

1

Я подозреваю, что вам нужно сделать mTemperature.setOnEditorActionListener, так как это EditText. Кроме того, проверьте значение null на событии, actionId может быть равно EditorInfo.IME_ACTION_DONE, когда событие равно null.