我有复选框列表,并且每次单击显示消息按钮时都想打印其名称。该消息显示为android.widget.Toast。如果选中了多个复选框,我希望将所有复选框打印在一个字符串中。我正在使用switch-case语句来确定单击了哪个复选框,并在某些情况下将新名称添加到我的结果字符串中。
public styles = StyleSheet.create({
scroll_layouts: {
height: 100,
zIndex: 40,
backgroundColor: "#fff",
},
Lay_view: {
width: 100,
height: 200,
transform: [
{ translateX: 0},
{ translateY: 0},
]
}
});
public displayLayout()
{
if(this.props.index == 2)
{
var to_disp = [];
var all_layouts = this.props.data_string.split('&');
for(var j = 0;j < all_layouts.length - 1;j++)
{
var all_dim = all_layouts[j].split(";");
var xyz=[];
var images_arr = this.props.arr_img;
for(var i = 0;i < all_dim.length - 1;i++)
{
var image_fill = images_arr[i].uri;
var indivi_elems = all_dim[i].split(',');
var h = parseFloat(indivi_elems[2]);
var w = parseFloat(indivi_elems[3]);
var xc = parseFloat(indivi_elems[0]);
var yc = parseFloat(indivi_elems[1]);
xyz.push(<Image key = {i} source = {{uri: image_fill}} style = {{height: h, width: w,transform: [{ translateX: xc},{ translateY: yc},{scaleX:1},{scaleY:1}]}}/>);
}
var hjk = <View key = {j} style = {this.styles.Lay_view}>{xyz}</View>;
to_disp.push(hjk);
}
return (
<ScrollView horizontal = {true} style = {this.styles.scroll_layouts}>
{to_disp}
</ScrollView>)
}
}
我在这里遇到问题。结果字符串是在课程开始时创建的,并且在第一次单击按钮之后创建了结果字符串;对于第二次单击,即使未选中它们也包含相同的项目名称。我可以添加更多项目,但结果字符串永远不会重置。
case R.id.cbox_choclate_syrup:
if (checked)
tag = tag + "chocolate syrup ";
break;
如上所示,我尝试在属于button的onClick方法末尾重置结果字符串。在这种情况下,即使我没有取消选中它们,以前单击的项目也会在第二次单击中被删除。
public void tosMessage(String message){
Context context = getApplicationContext();
CharSequence msg = message;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, msg, duration);
toast.show();
// resetting the result string.
tag = "Toppings: ";
}
我希望每次单击按钮时仅打印和排他检查的项目名称。请帮助我找到解决方案。
答案 0 :(得分:0)
您应该首先检查是否已选择某些内容,然后根据该内容更改String。我也改变了您的逻辑。
public class MainActivity extends AppCompatActivity {
public static String tag = "Toppings: ";
//KEEP REFERENCE TO SELECTIONS
boolean isCboxChoclateSyrupChecked = false;
boolean isCboxSprinklesChecked = false;
boolean isCboxCrushedNutsChecked = false;
boolean isCboxCherriesChecked = false;
boolean isCboxOrioCookieCrumblesChecked = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onCheckboxClicked(View view) {
// is the view now checked.
boolean checked = ((CheckBox) view).isChecked();
// check which checkbox is checked?
switch (view.getId()){
case R.id.cbox_choclate_syrup:
if (checked){
isCboxChoclateSyrupChecked = true;
}else{
isCboxChoclateSyrupChecked = false;
}
break;
case R.id.cbox_sprinkles:
if (checked){
isCboxSprinklesChecked = true;
}else{
isCboxSprinklesChecked = false;
}
break;
case R.id.cbox_crushed_nuts:
if (checked){
isCboxCrushedNutsChecked = true;
}else{
isCboxCrushedNutsChecked = false;
}
break;
case R.id.cbox_cherries:
if (checked){
isCboxCherriesChecked = true;
}else{
isCboxCherriesChecked = false;
}
break;
case R.id.cbox_orio_cookie_crumbles:
if (checked){
isCboxOrioCookieCrumblesChecked = true;
}else{
isCboxOrioCookieCrumblesChecked = false;
}
break;
}
}
public void tosMessage(String messageTag){
String getAllSelected = "";
if (isCboxChoclateSyrupChecked){
getAllSelected = getAllSelected + "chocolate syrup ";
}
if (isCboxSprinklesChecked){
getAllSelected = getAllSelected + "sprinkles ";
}
if (isCboxCrushedNutsChecked){
getAllSelected = getAllSelected + "crushed nuts ";
}
if (isCboxCherriesChecked){
getAllSelected = getAllSelected + "cherries ";
}
if (isCboxOrioCookieCrumblesChecked){
getAllSelected = getAllSelected + "orio cookie crumbles ";
}
Toast toast = Toast.makeText(getApplicationContext(), messageTag+" "+getAllSelected, Toast.LENGTH_SHORT);
toast.show();
}
public void showMsg(View view) {
tosMessage(tag);
}
}
如果您选择樱桃和巧克力粉,您的吐司面包应该显示:
Toppings: cherries sprinkles
答案 1 :(得分:0)
首先在Java文件中声明复选框
CheckBox cbox_choclate_syrup,cbox_sprinkles,cbox_crushed_nuts,cbox_cherries, cbox_orio_cookie_crumbles;
然后复制并粘贴以下功能:
private void setupFindViewById(){
CheckBox cbox_choclate_syrup = findViewById(R.id.cbox_choclate_syrup);
cbox_sprinkles = findViewById(R.id.cbox_sprinkles);
cbox_crushed_nuts = findViewById(R.id.cbox_crushed_nuts);
cbox_cherries = findViewById(R.id.cbox_cherries);
cbox_orio_cookie_crumbles = findViewById(R.id.cbox_orio_cookie_crumbles);
}
private CheckBox[] getCheckBoxArray(){
return new CheckBox[]{
CheckBox cbox_choclate_syrup,cbox_sprinkles,cbox_crushed_nuts,cbox_cherries, cbox_orio_cookie_crumbles
};
}
在您的onCreate方法中,调用setupFindViewById()
现在将您的onCheckboxClicked
方法替换为以下内容:
public void onCheckboxClicked(View view) {
tag = "Toppings: ";
for(CheckBox checkBox: getCheckBoxArray()){
boolean checked = checkBox.isChecked();
switch (checkBox.getId()){
case R.id.cbox_choclate_syrup:
if (checked)
tag = tag + "chocolate syrup ";
break;
case R.id.cbox_sprinkles:
if (checked)
tag = tag + "sprinkles ";
break;
case R.id.cbox_crushed_nuts:
if (checked)
tag = tag + "crushed nuts ";
break;
case R.id.cbox_cherries:
if (checked)
tag = tag + "cherries ";
break;
case R.id.cbox_orio_cookie_crumbles:
if (checked)
tag = tag + "orio cookie crumbles ";
break;
}
}
}
现在你可以去了:
我做了什么:首先,我们需要与您的小部件相对应的变量。然后,我们创建了一个函数getCheckBoxArray()
,该函数将返回复选框数组。在onCheckboxClicked
方法中,因为要检查每个复选框的状态,所以我们遍历数组
评论中提到的问题:
但是,在第二次单击按钮时它不起作用。让我解释; 当我选择樱桃和洒,然后单击显示消息时 按钮,烤面包完美搭配我选择的两个浇头 与复选框。但复选框上没有任何链条(全部 复选框保持与第一次单击相同),然后在第二次单击 按钮,烤面包片显示为空。我想我需要一种方法来重设我的 标签,但是在哪里?
之所以会这样,是因为tag变量的值设置为tag = "Toppings: ";
在tosMessage(String message)
方法中。如果您从该方法中删除该行,则应该可以正常工作。
答案 2 :(得分:0)
如果view.getId()
等于其中一种情况,则case
中的语句将运行,而break
将终止整个switch-case
,因此应用程序不会检查其他{{ 1}} s。因此,您必须用CkeckBox
替换switch语句。
if-else-if
注意:删除break语句将不起作用。
答案 3 :(得分:0)
将 ch1,ch2,ch3,ch4,ch5 声明为 Checkbox 。并将 message 作为私有和全局 String ,将其值分配给 "Toppings:" 。
CheckBox ch1,ch2,ch3,ch4,ch5;
private String message = "Toppings: ";
在 onCraete 方法中,通过 findViewById() 获取 ch1,ch2,ch3,ch4,ch5 的引用。
ch1 = findViewById(R.id.checkBox_chocolate_syrup);
ch2 = findViewById(R.id.checkBox_sprinkles);
ch3 = findViewById(R.id.checkBox_crushed_nuts);
ch4 = findViewById(R.id.checkBox_cherries);
ch5 = findViewById(R.id.checkBox_orio_cookie);
创建一个名为 showToast() 的自定义方法。
public void showToast () {
if (ch1.isChecked()) {
message += ch1.getText().toString() + " ";
}
if (ch2.isChecked()) {
message += ch2.getText().toString() + " ";
}
if (ch3.isChecked()) {
message += ch3.getText().toString() + " ";
}
if (ch4.isChecked()) {
message += ch4.getText().toString() + " ";
}
if (ch5.isChecked()) {
message += ch5.getText().toString() + " ";
}
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
我们定义了另一个方法 displayToast() 作为 android:onClick 属性的值,通过按钮点击来显示 toast。现在我们将调用它的showToast()方法来显示toast,然后我们将重置消息字符串以准备下次点击。
public void displayToast(View view) {
showToast();
message = "Toppings: ";
}