Cod*_*der 26 android android-menu android-actionbar
我有动作栏menuitems取消并保存.
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>
Run Code Online (Sandbox Code Playgroud)
我想在活动开始时禁用保存menuitem.
我的活动代码 -
@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{
}
}
Run Code Online (Sandbox Code Playgroud)
所以我在这里要做的是,最初当活动开始时,应该禁用保存菜单项,当editext包含一些文本时,应该启用它.
我不确定configureSaveButton方法中的if中的代码应该是什么.另外我如何最初禁用保存菜单项.
我在onPrepareOptionsMenu中得到空指针异常.
我使用的是android 4.1
Pra*_*tik 28
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.addprojectmenu, menu);
menu.getItem(0).setEnabled(false); // here pass the index of save menu item
return super.onPrepareOptionsMenu(menu);
}
Run Code Online (Sandbox Code Playgroud)
只是它充气的准备时间和膨胀的菜单后,禁用不需要inflate的oncreateoptionemenu时候,或者你也可以只使用最后两行代码膨胀之后onCreateOptionMenu.
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.getItem(0).setEnabled(false); // here pass the index of save menu item
return super.onPrepareOptionsMenu(menu);
}
Run Code Online (Sandbox Code Playgroud)
小智 15
我发现这篇文章是因为我想达到同样的效果.其他答案都没有完全有助于我的工作.经过一些研究和反复试验,我得到了我的工作.所以我决定分享我的结果.
我为此任务创建的变量:
MenuItem save_btn;
boolean b = false;`
Run Code Online (Sandbox Code Playgroud)
然后设置操作栏菜单:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.update_menu_item, menu);
save_btn = (MenuItem) menu.findItem(R.id.action_save);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu){
save_btn.setEnabled(b);
super.onPrepareOptionsMenu(menu);
return true;
}
Run Code Online (Sandbox Code Playgroud)
由于变量boolean b初始化为false,save_btn因此在创建活动时禁用该变量.
以下是切换save_btn使用@OpenSourceRulzz示例的方法:
private void updateSaveButton (CharSequence s){
String text = null;
if(s != null){
text = s.toString();
}
if(text != null && text.trim().length() != 0){
b = true;
}
else{
b = false;
}
}
Run Code Online (Sandbox Code Playgroud)
使用@OpenSourceRulzz示例再次通过框中的TextWatcher()函数调用此方法EditTextonCreate()
@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) {
updateSaveButton(s);
invalidateOptionsMenu();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after){}
@Override
public void afterTextChanged(Editable s) {}
});
}
Run Code Online (Sandbox Code Playgroud)
最后一块拼图是添加invalidateOptionsMenu()方法.
我提出的使我的工作的部分是使用boolean b变量来切换状态save_btn.
| 归档时间: |
|
| 查看次数: |
42454 次 |
| 最近记录: |