【发布时间】:2017-04-17 14:28:54
【问题描述】:
您好,我想创建一个列表。在工具栏上长按将显示选择全部和删除选定的选项。我不知道我是否应该 RadioGroup 和隐藏按钮或使用 listView 并创建自己的行示例并在那里添加单选按钮。
【问题讨论】:
您好,我想创建一个列表。在工具栏上长按将显示选择全部和删除选定的选项。我不知道我是否应该 RadioGroup 和隐藏按钮或使用 listView 并创建自己的行示例并在那里添加单选按钮。
【问题讨论】:
默认的标准 android 行为是上下文操作栏(我可以解释)应该在用户长按项目列表时出现 如
众多资源之一是
https://androidkennel.org/contextual-toolbar-actionbar-tutorial/
【讨论】:
我不能说得很具体,我可以说,通常为了实现您自己的特定目标,创建自己的行将证明对您的最终目标有益。而不是隐藏 RadioGroup
【讨论】:
我对 menuInflater 的理解有点问题。此类将菜单 XML 文件实例化为 Menu 对象。但是那里设置了新菜单
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) { //when this method is going to be made? Menu is int the toolbar and ListView isn't connected with toolbar so which menu I get in the next next line?
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.toolbar_cab, menu); // in this line set a new menu
return true;
}`
【讨论】:
从 RadioGroup 中隐藏 RadioButton 非常简单。您只需编写 btnRadio1.setVisibility(View.INVISIBLE);。但是你必须知道这条规则:例如,如果你在 RadioGroup 中有 4 个 RadioButtons,你只能以相反的顺序使它们不可见!我的意思是它们在您的布局 .xml 文件中的 RadioGroup 中定义的顺序。不可能只隐藏 btnRadio3,而 btnRadio4 是可见的!您必须隐藏 btnRadio3 和 btnRadio4。或者只有 btnRadio4。所以,如果你想隐藏 1 个按钮,那就是按钮 4。如果你想隐藏 2 个按钮 - 它们是 4 和 3。如果你想隐藏 3 个按钮,它们是 4、3 和 2。所有其他组合,只需不起作用。 这是我的测验应用程序中的代码,每个问题可能有 2 到 6 个答案。当前问题的答案存储在字符串数组 answers [] 中。
RadioButton btnAnswer1;
RadioButton btnAnswer2;
RadioButton btnAnswer3;
RadioButton btnAnswer4;
RadioButton btnAnswer5;
RadioButton btnAnswer6;
RadioGroup radioGroup;
// onCreate activity
btnAnswer1 = (RadioButton) findViewById(R.id.btnAnswer1);
btnAnswer2 = (RadioButton) findViewById(R.id.btnAnswer2);
btnAnswer3 = (RadioButton) findViewById(R.id.btnAnswer3);
btnAnswer4 = (RadioButton) findViewById(R.id.btnAnswer4);
btnAnswer5 = (RadioButton) findViewById(R.id.btnAnswer5);
btnAnswer6 = (RadioButton) findViewById(R.id.btnAnswer6);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.clearCheck();
btnAnswer1.setVisibility(View.VISIBLE);
btnAnswer2.setVisibility(View.VISIBLE);
numberOfAnswers = 2; //at least 2 answers
//if 3-d element is empty i.e. 2 answers only
//i.e. buttons 3,4,5,6 must be hidden
if (answers[2].isEmpty()) {
btnAnswer3.setVisibility(View.INVISIBLE);
btnAnswer4.setVisibility(View.INVISIBLE);
btnAnswer5.setVisibility(View.INVISIBLE);
btnAnswer6.setVisibility(View.INVISIBLE);
} else {
btnAnswer3.setVisibility(View.VISIBLE);
numberOfAnswers = 3;
}
if (answers[3].isEmpty()) {
btnAnswer4.setVisibility(View.INVISIBLE);
btnAnswer5.setVisibility(View.INVISIBLE);
btnAnswer6.setVisibility(View.INVISIBLE);
} else {
btnAnswer4.setVisibility(View.VISIBLE);
numberOfAnswers = 4;
}
if (answers[4].isEmpty()) {
btnAnswer5.setVisibility(View.INVISIBLE);
btnAnswer6.setVisibility(View.INVISIBLE);
} else {
btnAnswer5.setVisibility(View.VISIBLE);
numberOfAnswers = 5;
}
if (answers[5].isEmpty()) {
btnAnswer6.setVisibility(View.INVISIBLE);
} else {
btnAnswer6.setVisibility(View.VISIBLE);
numberOfAnswers = 6;
}
这里是xml文件:
<ScrollView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginLeft="5dip"
android:orientation="vertical">
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/btnAnswer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/btnAnswer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/btnAnswer3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/btnAnswer4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/btnAnswer5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/btnAnswer6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RadioGroup>
</ScrollView>
【讨论】: