我正在研究这个简单的Android应用程序。我在操作栏上有三个菜单项,每个菜单项在点击它们时会膨胀3个不同的片段。
我的问题:当我点击其中一个菜单项时,我希望菜单项的图标更改为drawables文件夹中的另一个图标并保持这样,直到我点击另一个菜单项,然后它将更改为第一个图标在我点击它之前。
我尝试使用带有“state_pressed”的选择器.xml,当我点击菜单项时,它会更改图标一秒钟,但一旦我停止点击它就会返回到第一个图标。您可以在下面找到相关代码。如果你能帮助我,我会很感激。
submenu.xml (操作栏菜单):
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" >
<item
android:id="@+id/action_todo"
android:title=""
android:icon="@drawable/action_bar_selector"
app:showAsAction="always" />
</menu>
action_bar_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/ic_check_box_white_24dp"
android:state_pressed="true" />
<item
android:drawable="@drawable/ic_check_box_white_24dp_unpressed" />
</selector>
ProjectTodo.java (在片段类中夸大操作栏):
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
inflater.inflate(R.menu.submenu, menu);
}
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(id == R.id.action_back){
ProjectDetails frag = new ProjectDetails();
ProjectMainListFragmentChanger fragInterface = (ProjectMainListFragmentChanger) getActivity();
fragInterface.projectMainListChangeFragment(frag);
return false;
}
if(id == R.id.action_details){
return true;
}
if(id == R.id.action_todo){
ProjectDetails frag = new ProjectDetails();
ProjectTodoFragmentChanger fragInterface = (ProjectTodoFragmentChanger) getActivity();
fragInterface.projectTodoChangeFragment(frag);
return false;
}
return super.onOptionsItemSelected(item);
}
您可以看到当前应用程序的快速.gif图像。当我点击菜单项时,它只会更改为第二个图标一秒钟,然后返回其原始图标:
答案 0 :(得分:2)
如果您只想切换商品,可以使用<button id="myBtn" onclick="langToggle()" value="English">change language<i class="fa fa-globe"></i></button>
<section id="contenten" class="show">
English
</section>
<section id="contentnl" class="hide">
Dutch
</section>
和onOptionsItemSelected
onPrepareOptionsMenu
答案 1 :(得分:-2)
就这么简单!
ToobarActivity.class:
public class ToolbarActivity extends AppCompatActivity {
private Menu menu;// Global Menu Declaration
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toolbar);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//This is my Back Button View onClick on toolbar
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
this.menu = menu;
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//This is the Answer to your problem
if (id == R.id.actionsignout) {
menu.getItem(1).setVisible(true);
menu.getItem(0).setVisible(false);
return true;
}else if (id == R.id.action) {
menu.getItem(0).setVisible(true);
menu.getItem(1).setVisible(false);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
}
在res / menu / main_menu.xml上:
将第二个项目“可见性”设置为 false
<?xml version="1.0" encoding="utf-8"?>
<item android:id="@+id/actionsignout"
app:showAsAction="always"
android:icon="@drawable/ic_pause_black_24dp"
android:title="Sign Out"/>
<item android:id="@+id/action"
app:showAsAction="ifRoom"
android:visible="false"
android:icon="@drawable/ic_play_arrow_black_24dp"
android:title=""/>