1

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

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

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

编辑文本:

Egg
Hotdog

那可能吗?谢谢你的帮助!

4

2 回答 2

2
      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;
}

  }
于 2013-08-28T22:51:51.403 回答
1

你可以像这样使用 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);
        }
    }
  });

}

我只为一个人做希望这有帮助

于 2013-08-28T22:29:28.930 回答