Android复选框已选中无效

时间:2014-08-03 00:58:06

标签: java android android-checkbox

所以我有这个活动,我有2个复选框,当用户点击其中一个时,它将被检查并烘烤文本“11111”和“222222”当用户加入活动时他们将获得祝酒词“欢迎”。现在,Welcome toast正在运行,但if.s doesent似乎有效。可能是什么问题?

这是我的代码

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();;
    }


}

2 个答案:

答案 0 :(得分:0)

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();;
}

}

答案 1 :(得分:0)

您可以实现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;
            }
        }

    });

快乐的编码! :d