Java 使用复选框。复选框字符串值在选中时将传输到edittext
假设我有3个复选框,每个复选框都有一个对应的字符串值。我想要的是,当我选中复选框1和2时,它们对应的字符串或值将全部放在edittext上Java 使用复选框。复选框字符串值在选中时将传输到edittext,java,android,checkbox,android-edittext,Java,Android,Checkbox,Android Edittext,假设我有3个复选框,每个复选框都有一个对应的字符串值。我想要的是,当我选中复选框1和2时,它们对应的字符串或值将全部放在edittext上 Example: [✓] Checkbox 1 (Egg) [✓] Checkbox 2 (Hotdog) [ ] Checkbox 3 (Cheese) 当我选中复选框1和2时。它将转到edittext并以“鸡蛋”\n+“热狗”或类似方式查看。 输出: 编辑文本: Egg Hotdog 可能吗?谢谢你的帮助 您可以像这样使用setOnClickList
Example:
[✓] Checkbox 1 (Egg)
[✓] Checkbox 2 (Hotdog)
[ ] Checkbox 3 (Cheese)
当我选中复选框1和2时。它将转到edittext并以“鸡蛋”\n+“热狗”或类似方式查看。
输出:
编辑文本:
Egg
Hotdog
可能吗?谢谢你的帮助 您可以像这样使用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);
}
}
});
}
我只做了一件事
希望这有帮助不管是什么,我相信这是可能的。我不太清楚你的意思,事情是这样的。我知道它很简单,但我不知道它是如何实现的。@Cruncher复选框1、2和3有特定的值。它就像一个订购系统。如果我选择egg和hotdog并点击“Save”(保存)按钮,选中项的所有值都将传输到edittext中。edittext是一个换行分隔的文本框,每行包含一个选中的复选框吗?我不是android专家,但你需要为复选框的onclick/onchange编写一个事件处理程序。此时,通过读取选中值来完全更新edittext,并且所有复选框的文本值我觉得onClick太复杂了。为什么复选框的名称需要if语句?一般来说,您可以通过获取复选框上的文本并将其附加到。。替换也是一样。嘿,伙计,是的,你说得对!当我回答这个问题时,我很困。我怎样才能将它保存在数据库中?你的意思是将字符串保存在数据库中?我认为您不希望从onClick事件中执行此操作,您可能需要将该任务委托给其他类。无论哪种方式,答案都不适合注释。不,我不是将其保存在
单击事件中,而是将值保存在saveDataLocal()
函数中
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;
}
}