【问题标题】:Working with checkbox. Checkbox string value will transfer to edittext when checked使用复选框。选中时复选框字符串值将传输到edittext
【发布时间】:2016-03-10 19:59:53
【问题描述】:

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

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

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

编辑文本:

Egg
Hotdog

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

【问题讨论】:

  • 不管它是什么,我确信它是可能的。不过我不完全确定你的意思。
  • 是这样的。我知道它很简单,但我不知道怎么做。
  • @Cruncher 复选框 1、2 和 3 具有特定值。它就像一个订购系统。如果我选择鸡蛋和热狗并点击“保存”按钮,所有选中项的值都将传输到编辑文本中。
  • edittext 是换行符分隔的文本框,每行包含 1 个选中的复选框吗?
  • 我不是 android 专家,但您需要为复选框的 onclick/onchange 编写事件处理程序。那时通过读取选中的值和所有复选框的文本值来完全更新edittext

标签: java android checkbox android-edittext


【解决方案1】:
      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;
}

  }

【讨论】:

  • 我觉得你的 onClick 太复杂了。为什么需要为复选框名称添加 if 语句?通常,您可以通过获取复选框上的文本并附加它来做到这一点。与替换相同。
  • 嘿伙计,你是对的!当我给出这个答案时,我很困
【解决方案2】:

你可以像这样使用 setOnClickListener

CheckBox chkEgg;
EditText yourEditText;
String yourText;
public void addListenerOnChkIos() {

    chkEgg = (CheckBox) findViewById(R.id.checkbox1);

    chkEgg.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
                //is chkEgg checked?
        if (((CheckBox) v).isChecked()) {
            yourText = yourText + "Egg";
            yourEditText.setText(yourText);
        }
    }
  });

}

我只为一个 希望这会有所帮助

【讨论】:

  • 如何将其保存在我的数据库中?
  • 您的意思是将字符串保存在数据库中?我认为您不想从 onClick 事件中执行此操作,您可能需要将该工作委托给其他类。无论哪种方式,答案都不适合 cmets。
  • 不,我没有将其保存在click event 中,而是将值保存在我的saveDataLocal() 函数中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-11
  • 2016-04-26
  • 2012-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多