Mam menu paska zadań, anuluj i zapisz.Włącz/wyłącz menu ActionBar Pozycja
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/saveButton"
android:showAsAction="always"
android:title="@string/save"
android:visible="true">
</item>
<item android:id="@+id/cancelButton"
android:showAsAction="always"
android:title="@string/cancel"
android:visible="true">
</item>
</menu>
Chcę wyłączyć zapisać menuitem gdy działalność jest uruchomiony.
Mój kod aktywność -
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_project);
EditText projectName = (EditText) findViewById(R.id.editTextProjectName);
projectName.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
configureSaveButton(s);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
@Override
public void afterTextChanged(Editable s) {}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.addprojectmenu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item = (MenuItem) findViewById(R.id.saveButton);
item.setEnabled(false);
return super.onPrepareOptionsMenu(menu);
}
private void configureSaveButton(CharSequence s){
String text = null;
if(s != null){
text = s.toString();
}
if(text != null && text.trim().length() != 0){
}else{
}
}
Więc co usiłuję zrobić tutaj jest, gdy początkowo działalność rozpoczęła Zapisz menu powinno być wyłączone, a gdy editext zawiera jakiś tekst to powinno być włączone.
Nie jestem pewien, jaki powinien być kod, jeśli jeszcze w metodzie configureSaveButton. Również jak mogę wyłączyć zapisywanie pozycji menu początkowo.
Otrzymuję wyjątek wskaźnika pustego w onPrepareOptionsMenu.
Używam Android 4.1
Wypróbuj ten jeden najlepszy sposób na wyłączenie ikony pozycji menu http://stackoverflow.com/a/33910273/4449159 –