以编程方式更改PopupMenu项的标题 [英] Change PopupMenu items' title programmatically
本文介绍了以编程方式更改PopupMenu项的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有4个选项的PopupMenu:添加为朋友,邻居,同事和粉丝.我想做的是,如果您单击添加为朋友",然后将该选项更改为删除朋友".
I have a PopupMenu with 4 options: add as friend, as neighbour, as workmate and as fan. What I want to do is if you click, lets say, on "Add as friend", then that option changes into "remove friend".
这是我到目前为止尝试过的:
Here's what I've tried so far:
活动:
private Menu menuOpts;
public void showPopup(View v) {
Context wrapper = new ContextThemeWrapper(this, R.style.PopupPerfilAjeno);
PopupMenu popup = new PopupMenu(wrapper, v);
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.menu);
popup.show();
menuOpts = popup.getMenu();
}
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_friend:
String add_as_friend = getString(R.string.add_as_friend );
if (item.getTitle().toString().equals(add_as_friend )) {
addContact(1, item);
}
else {
removeContact(1, item);
}
return true;
case R.id.add_workmate:
//
return true;
case R.id.add_neighbour:
//
return true;
case R.id.add_fan:
//
return true;
default:
return false;
}
}
// circle: 1 = friend, 2 = workmate, 3 = neighbour, 4 = fan
private void addContact(final int circle, final MenuItem item) {
switch (circle) {
case 1:
menuOpts.findItem(R.id.add_friend).setTitle(R.string.remove_friend);
// this won't work either:
// item.setTitle(R.string.remove_friend);
break;
case 2:
menuOpts.findItem(R.id.add_workmate).setTitle(R.string.remove_workmate);
break;
case 3:
menuOpts.findItem(R.id.add_neighbour).setTitle(R.string.remove_neighbour);
break;
case 4:
menuOpts.findItem(R.id.add_fan).setTitle(R.string.remove_fan);
break;
}
}
menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/add_friend"
android:orderInCategory="100"
android:title="@string/add_as_friend"
app:showAsAction="always" />
<item
android:id="@+id/add_workmate"
android:orderInCategory="100"
android:title="@string/add_as_workmate"
app:showAsAction="always" />
<item
android:id="@+id/add_neighbour"
android:orderInCategory="100"
android:title="@string/add_as_neighbour"
app:showAsAction="always" />
<item
android:id="@+id/add_fan"
android:orderInCategory="100"
android:title="@string/add_as_fan"
app:showAsAction="always" />
</menu>
推荐答案
我找到了解决方案.我添加了4个布尔变量,并修改了showPopup和addContact方法,如下所示:
I've found a solution. I added 4 boolean variables and modified the showPopup and addContact methods like this:
private boolean friend, workmate, neighbour, fan;
public void showPopup(View v) {
Context wrapper = new ContextThemeWrapper(this, R.style.PopupPerfilAjeno);
PopupMenu popup = new PopupMenu(wrapper, v);
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.menu);
Menu menuOpts = popup.getMenu();
if (friend) {
menuOpts.getItem(0).setTitle(R.string.remove_friends);
}
if (workmate) {
menuOpts.getItem(1).setTitle(R.string.remove_workamtes);
}
if (neighbour) {
menuOpts.getItem(2).setTitle(R.string.remove_neighbours);
}
if (fan) {
menuOpts.getItem(3).setTitle(R.string.remove_fans);
}
popup.show();
}
private void addContact(final int circle) {
switch (circle) {
case 1:
friend = true;
workmate = false;
neighbour = false;
fan = false;
break;
case 2:
friend = false;
workmate = true;
neighbour = false;
fan = false;
break;
case 3:
friend = false;
workmate = false;
neighbour = true;
fan = false;
break;
case 4:
friend = false;
workmate = false;
neighbour = false;
fan = true;
break;
}
}
这篇关于以编程方式更改PopupMenu项的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文