[英]Android checkbox is checked not working
so i have this Activity and i have 2 checkboxes , when the user clicks one of them it will be checked and toast a text "11111" and "222222" also when the user joins the activity they will get the toast "Welcome". 因此,我有此活动,并且我有2个复选框,当用户单击其中的一个复选框时,它将被选中并吐司一个文本“ 11111”和“ 222222”,当用户加入该活动时,他们将得到吐司“欢迎”。 Now , the Welcome toast is working but the if.s doesent seem to work.
现在,“欢迎吐司”正在运行,但是if.s确实起作用了。 What could be the problem?
可能是什么问题呢?
Here is my code 这是我的代码
package com.myapp.pack;
import com.myapp.pack.R;
import com.pushbots.push.Pushbots;
import android.R.string;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.Toast;
public class settup extends Activity {
private String SENDER_ID = "xxx";
private String PUSHBOT_ID = "xxx";
public static final String PREFS_NAME = "BuyMeCheckBoxes";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notifsettings);
Pushbots.init(this, SENDER_ID , PUSHBOT_ID);
Pushbots.getInstance().setMsgReceiver(GoToApp.class);
Pushbots.getInstance().setRegStatus(true);
Toast.makeText(settup.this,
"A-ti accesat setarile app", Toast.LENGTH_LONG).show();
CheckBox auto = (CheckBox) findViewById(R.id.checkBox1);
CheckBox imobiliare = (CheckBox) findViewById(R.id.checkBox2);
if (auto.isChecked()) {
auto.setChecked(false);
Toast.makeText(settup.this,
"1111111", Toast.LENGTH_LONG).show();
}
if (imobiliare.isChecked()) {
auto.setChecked(false);
Toast.makeText(settup.this,
"2222222", Toast.LENGTH_LONG).show();
}
// Array of choices
// String colors[] = {"Toata Tara","Oradea","Timisoara","Bucuresti","Cluj-Napoca","Constanta","Iasi","Suceava","Targu Mures"};
// Selection of the spinner
// Application of the Array to the Spinner
// ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, colors);
// spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
// spinner.setAdapter(spinnerArrayAdapter);
}
protected void onResume() {
super.onResume();
final CheckBox auto = (CheckBox) findViewById(R.id.checkBox1);
final CheckBox imobiliare = (CheckBox) findViewById(R.id.checkBox2);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean c1 = settings.getBoolean("auto", false);
boolean c2 = settings.getBoolean("imobiliare", false);
auto.setChecked(c1);
imobiliare.setChecked(c2);
}
@Override
protected void onStop(){
super.onStop();
final CheckBox auto = (CheckBox) findViewById(R.id.checkBox1);
final CheckBox imobiliare = (CheckBox) findViewById(R.id.checkBox2);
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
boolean checkBoxValue1 = auto.isChecked();
boolean checkBoxValue2 = imobiliare.isChecked();
editor.putBoolean("auto", checkBoxValue1);
editor.putBoolean("imobiliare", checkBoxValue2);
editor.commit();;
}
}
public class settup extends Activity {
private String SENDER_ID = "xxx";
private String PUSHBOT_ID = "xxx";
public static final String PREFS_NAME = "BuyMeCheckBoxes";
final CheckBox auto = null;
final CheckBox imobiliare = null;
@Override
protected void onCreateView(Bundle savedInstanceState) {
super.onCreateView(savedInstanceState);
auto = (CheckBox) findViewById(R.id.checkBox1);
imobiliare = (CheckBox) findViewById(R.id.checkBox2);
if (auto.isChecked()) {
auto.setChecked(false);
Toast.makeText(settup.this,
"1111111", Toast.LENGTH_LONG).show();
}
if (imobiliare.isChecked()) {
auto.setChecked(false);
Toast.makeText(settup.this,
"2222222", Toast.LENGTH_LONG).show();
}
// Array of choices
// String colors[] = {"Toata Tara","Oradea","Timisoara","Bucuresti","Cluj-Napoca","Constanta","Iasi","Suceava","Targu Mures"};
// Selection of the spinner
// Application of the Array to the Spinner
// ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, colors);
// spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
// spinner.setAdapter(spinnerArrayAdapter);
}
protected void onResume() {
super.onResume();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean c1 = settings.getBoolean("auto", false);
boolean c2 = settings.getBoolean("imobiliare", false);
auto.setChecked(c1);
imobiliare.setChecked(c2);
}
@Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
boolean checkBoxValue1 = auto.isChecked();
boolean checkBoxValue2 = imobiliare.isChecked();
editor.putBoolean("auto", checkBoxValue1);
editor.putBoolean("imobiliare", checkBoxValue2);
editor.commit();;
}
} }
You can implement an OnClickListener, so for your case it should be something like this 您可以实现OnClickListener,因此对于您的情况,应该是这样的
----------------------------UPDATE------------------------- ----------------------------更新--------------------- ----
//ensure activity implements on click listener
public class settup extends Activity implements View.OnClickListener {
//set listener - put in onCreate method
auto.setOnClickListner(this);
//set up logic - also goes in onCreate method
auto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
final boolean isChecked = image.isChecked();
switch (arg0.getId()) {
case R.id.checkBox1:
if (isChecked) {
Toast.makeText(getApplicationContext(),
"1111111", Toast.LENGTH_LONG).show();
}
break;
}
}
});
Happy coding! 编码愉快! :D
:D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.