Android如何将值从复选框传递到当前视图
我想将选中复选框中的值传递给Toast视图 但是看看它是怎么显示的你能帮我吗Android如何将值从复选框传递到当前视图,android,Android,我想将选中复选框中的值传递给Toast视图 但是看看它是怎么显示的你能帮我吗 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText name=(EditText)findViewById(R.id.name); fi
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText name=(EditText)findViewById(R.id.name);
final EditText phone=(EditText)findViewById(R.id.phone_number);
final EditText address=(EditText)findViewById(R.id.address);
final RadioGroup shapes = (RadioGroup) findViewById(R.id.rBtnshapes);
final RadioGroup types = (RadioGroup) findViewById(R.id.rBtntypes);
final CheckBox pep = (CheckBox)findViewById(R.id.pepperoni);
final CheckBox mshr = (CheckBox)findViewById(R.id.mushrooms);
Button orderBtn = (Button)findViewById(R.id.orderBtn);
orderBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
StringBuffer result = new StringBuffer();
result.append("Pepperoni"+"\n").append(pep.isChecked()).toString();
result.append("Mushrooms"+"\n").append(mshr.isChecked()).toString();
int selectedShape = shapes.getCheckedRadioButtonId();
int selectedCheese = types.getCheckedRadioButtonId();
RadioButton sh = (RadioButton)findViewById(selectedShape);
RadioButton ty=(RadioButton)findViewById(selectedCheese);
Toast.makeText(MainActivity.this, "Name: "+name.getText()+"\n"+"Phone: "+phone.getText()+"\n"+"Address: " +address.getText()+"\n"+"Shape: "+sh.getText()+"\n"+"Cheese: "+ty.getText()+"\n"+"Toppings: "+result.toString(), Toast.LENGTH_SHORT).show();
}
});
}
}
我猜您希望选中的复选框如图所示
orderBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
StringBuffer result = new StringBuffer();
int selectedShape = shapes.getCheckedRadioButtonId();
int selectedCheese = types.getCheckedRadioButtonId();
RadioButton sh = (RadioButton)findViewById(selectedShape);
RadioButton ty=(RadioButton)findViewById(selectedCheese);
final CheckBox pep = (CheckBox)findViewById(R.id.pepperoni);
final CheckBox mshr = (CheckBox)findViewById(R.id.mushrooms);
if(pep.isChecked()==true){
result.append("Pepperoni"+"\n");
}
if(mshr.isChecked()==true){
result.append("Mushrooms"+"\n");
}
Toast.makeText(MainActivity.this, "Name: "+name.getText()+"\n"+"Phone: "+phone.getText()+"\n"+"Address: " +address.getText()+"\n"+"Shape: "+sh.getText()+"\n"+"Cheese: "+ty.getText()+"\n"+"Toppings: "+result.toString(), Toast.LENGTH_SHORT).show();
}
});
我想,而不是
orderBtn.setOnClickListener(new View.OnClickListener()
使用:
此方法将检查用户是否选中或取消选中对话框为复选框生成<代码>列表,并检查是否选中了<代码>复选框@
pep.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
String temp=buttonView.getText().toString();
}
}
});
然后将
字符串附加到StringBuffer
并将结果传递给toast消息。您希望得到什么输出?
pep.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
String temp=buttonView.getText().toString();
}
}
});