java - 使用复选框。复选框字符串值将在选中时传输到 edittext

标签 java android checkbox android-edittext

假设我有 3 个复选框,每个复选框都有一个对应的字符串值。我想要的是,当我选中复选框 1 和 2 时,它们对应的字符串或值将全部放在一个编辑文本上。

Example:
[✓] Checkbox 1 (Egg)
[✓] Checkbox 2 (Hotdog)
[ ] Checkbox 3 (Cheese)

当我选中框 1 和 2 时。它将转到编辑文本并查看为“Egg”\n +“Hotdog”或类似内容。 输出:

编辑文字:

Egg
Hotdog

这可能吗?感谢您的帮助!

最佳答案

      package com.example.checkboxes;

      import android.app.Activity;
      import android.os.Bundle;
      import android.view.Menu;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.widget.CheckBox;
      import android.widget.EditText;
      import android.widget.Toast;

       public class MainActivity extends Activity {
   private CheckBox egg, hotdog, cheese;
       private OnClickListener checkboxclicklistener;
       private EditText display;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    egg = (CheckBox) findViewById(R.id.a);
    cheese = (CheckBox) findViewById(R.id.c);
    hotdog = (CheckBox) findViewById(R.id.b);       
    display = (EditText) findViewById(R.id.display);


    createListener(egg);
    createListener(hotdog);
    createListener(cheese);



}

 public void createListener(CheckBox checkbox) {



        checkbox.setOnClickListener( new OnClickListener() {
            StringBuilder checkeditems = new StringBuilder();
            @Override
            public void onClick(View v) {
                 //is checkbox checked?


                if (((CheckBox) v).isChecked()) {

                    String checkboxname = ((CheckBox) v).getText().toString();

                    Toast.makeText(getApplicationContext(), checkboxname, Toast.LENGTH_LONG).show();

                    if(checkboxname.equalsIgnoreCase("egg"))
                    {
                        display.setText(display.getText().toString()+"\nEgg");
                    }

                    else if(checkboxname.equalsIgnoreCase("hotdog"))
                    {
                        display.setText(display.getText().toString()+"\nHotdog");
                    }

                    else if(checkboxname.equalsIgnoreCase("cheese"))
                    {
                        display.setText(display.getText().toString()+"\nCheese");
                    }





                }

                else if (((CheckBox) v).isChecked()==false)
                {
                   String checkboxname = ((CheckBox) v).getText().toString();

                    if(checkboxname.equalsIgnoreCase("egg"))
                    {
                        display.setText(display.getText().toString().replace("\nEgg", ""));
                    }

                    else if(checkboxname.equalsIgnoreCase("hotdog"))
                    {
                        display.setText(display.getText().toString().replace("\nHotdog", ""));
                    }

                    else if(checkboxname.equalsIgnoreCase("cheese"))
                    {
                        display.setText(display.getText().toString().replace("\nCheese", ""));
                    }
                }

              }
            });


 }





@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

  }

关于java - 使用复选框。复选框字符串值将在选中时传输到 edittext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18498296/

相关文章:

java - 设置 JPanel/JFrame 背景图片,我的第一次

java - 使用hibernate懒加载主要有哪些问题?

java - 在java中从JDBC连接到MySql

android - Android 设备上是否可以有 3 个或更多应用程序对用户可见?

java - Gradle protoc 插件找不到默认的 google proto 文件

java - 如何从 Android 中先前定义的字符串设置字符串?

android - 在android中按下按钮时如何删除某些元素

单击按钮时java清除表

javascript - 简化 JavaScript 代码

jquery - 以编程方式取消选中 jQuery UI 复选框按钮