繁体   English   中英

仅在选中时打印多个复选框名称

[英]Printing multiple checkbox names only when they are checked

我有复选框列表,每次单击显示消息按钮时都想打印他们的名字。 该消息显示为 android.widget.Toast。 如果选中多个复选框,我想将它们全部打印在一个字符串中。 我正在使用 switch-case 语句来确定单击了哪些复选框,并在某些情况下将新名称添加到我的结果字符串中。

case R.id.cbox_choclate_syrup:
                if (checked)
                    tag = tag + "chocolate syrup ";
                break;

在此处输入图片说明

我这里有问题。 结果字符串是在类的开头和第一次点击按钮后创建的,对于第二次点击它包含相同的项目名称,即使它们没有被选中。 我可以添加更多项目,但结果字符串永远不会被重置。

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: ";
    }

如上所示,我尝试在属于按钮的 onClick 方法的末尾重置结果字符串。 在这种情况下,即使我没有取消选中它们,先前选中的项目也会在第二次单击时被删除。

public class MainActivity extends AppCompatActivity {

    public static String tag = "Toppings: ";

    @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)
                    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;
        }
    }

    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();
        tag = "Toppings: ";
    }

    public void showMsg(View view) {
        tosMessage(tag);
    }
}

我希望每次单击按钮时只打印和专门检查的项目名称。 请帮我找到解决方案。

您应该首先检查是否已经选择了某些内容,然后根据它更改字符串。 我也稍微改变了你的逻辑。

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

首先在 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 = "Toppings: ";
tosMessage(String message)方法中。 如果您从此方法中删除该行,则它应该可以正常工作。

view.getId()等于的情形之一的,在该声明case运行,并break终止了整个switch-case ,因此应用程序没有检查其他CkeckBox秒。 所以你必须用if-else-if替换 switch 语句。

int id= view.getId();
if(id == R.id.cbox_choclate_syrup){
    if(checked)
        tag = tag + "chocolate syrup ";
}
else if(id == R.id.cbox_sprinkles){
    if(checked)
        tag = tag + "sprinkles ";
}
else if(id == R.id.cbox_crushed_nuts){
    if(checked)
        tag = tag + "crushed nuts ";
}
else if(id == R.id.cbox_cherries){
    if(checked)
        tag = tag + "cherries ";
}
else if(id == R.id.cbox_orio_cookie_crumbles){
    if(checked)
        tag = tag + "orio cookie crumbles ";
}

注意:删除 break 语句将不起作用。

将 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 属性的值,以通过 Button 单击显示 toast。 现在我们将调用它的 showToast() 方法来显示 toast,然后我们将重置消息字符串以准备下次点击。

public void displayToast(View view) {
    showToast();
    message = "Toppings: ";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM