Włącz / wyłącz pozycję menu paska akcji
Mam elementy menu paska zadań anulowane i zapisywane.
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ć zapisywanie menu po uruchomieniu aktywności.
Mój kod aktywności -
@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 to, co próbuję tutaj zrobić, to początkowo, kiedy uruchamiana jest aktywność, element menu powinien być wyłączony, a gdy editext zawiera jakiś tekst, powinien być włączony.
Nie jestem pewien, jaki powinien być kod w if else w metodzie configureSaveButton. Również jak mogę początkowo wyłączyć opcję menu zapisu.
Mam wyjątek wskaźnika zerowego w onPrepareOptionsMenu.
Używam Androida 4.1