我是Android开发的初学者。
我只想寻求有关我遇到的问题的帮助。
问题如下图所示。
应用程序显示错误的Toast文本,其中预期的Toast消息是:
Dog: True Cat: False Cow: False
但实际的Toast消息是:
Dog: True Cat: True Cow: False
以下是我的MainActivity Code:
package com.example.johnsethsalazar.checkbox;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private CheckBox c1, c2, c3; // Declaring Checkbox Variables.
private Button select;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
AddListenerToCheckBox1();
}
public void addListenerOnButton()
{
c1 = (CheckBox) findViewById(R.id.checkBox);
c2 = (CheckBox) findViewById(R.id.checkBox2);
c3 = (CheckBox) findViewById(R.id.checkBox3);
select = (Button) findViewById(R.id.button);
select.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
StringBuffer result = new StringBuffer();
result.append("Dog : ").append(c1.isChecked());//Gives True if Check and False if Not.
result.append("Cat : ").append(c2.isChecked());
result.append("Cow : ").append(c3.isChecked());
Toast.makeText(MainActivity.this, result.toString(), Toast.LENGTH_LONG).show();
}
});
}
public void AddListenerToCheckBox1()
{
c2 = (CheckBox) findViewById(R.id.checkBox);
c2.setOnClickListener(new View.OnClickListener()//Shows Toast("Dog is Selected") when Checkbox is Clicked.
{
@Override
public void onClick(View view)
{
if (((CheckBox) view).isChecked())
{
Toast.makeText(MainActivity.this, "Dog is Selected.", Toast.LENGTH_LONG).show();
}
}
});
}
}
答案 0 :(得分:2)
这是因为你先做
c1 = (CheckBox) findViewById(R.id.checkBox);
c2 = (CheckBox) findViewById(R.id.checkBox2);
c3 = (CheckBox) findViewById(R.id.checkBox3);
你的addListenerOnButton()中的是正确的,但是你调用的是AddListenerToCheckBox1()
c2 = (CheckBox) findViewById(R.id.checkBox);
用狗复选框覆盖c2的值。
我建议您将复选框命名为更人性化的名称,这样就不会让它们混淆。
命名约定会建议checkBoxDog,checkBoxCat,ect
答案 1 :(得分:0)
固定解决方案:
package com.example.johnsethsalazar.checkbox;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private CheckBox checkBoxDog, checkBoxCat, checkBoxCow;
private Button select;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
addListenerOnButton();
AddListenerToCheckBox1();
}
public void findViews() {
checkBoxDog = (CheckBox) findViewById(R.id.checkBox);
checkBoxCat = (CheckBox) findViewById(R.id.checkBox2);
checkBoxCow = (CheckBox) findViewById(R.id.checkBox3);
select = (Button) findViewById(R.id.button);
}
public void addListenerOnButton() {
select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
printState();
}
});
}
public void printState() {
StringBuilder result = new StringBuilder();
result.append("Dog : ").append(checkBoxDog.isChecked());//Gives True if Check and False if Not.
result.append("Cat : ").append(checkBoxCat.isChecked());
result.append("Cow : ").append(checkBoxCow.isChecked());
Toast.makeText(MainActivity.this, result.toString(), Toast.LENGTH_LONG).show();
}
public void AddListenerToCheckBox1() {
checkBoxDog.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(MainActivity.this, "Dog is Selected.", Toast.LENGTH_LONG).show();
}
}
});
}
}