如果选中复选框,如何显示Toast消息?

时间:2014-04-28 11:19:56

标签: android android-checkbox android-toast

我的checkbox

中有一个activity_main.xml

我有以下代码:

public class UpdateService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        BroadcastReceiver mReceiver = new ScreenReceiver();
        registerReceiver(mReceiver, filter);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        boolean screenOn = intent.getBooleanExtra("screen_state", false);
        if (!screenOn) {
            Toast.makeText(getApplicationContext(), "Screen is turned ON. Broadcast Received", Toast.LENGTH_LONG).show();
            //Want to display this toast message only in checkbox is checked
        } else {
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

我希望仅在Toastcheckbox时显示checked通知,否则不会显示。{/ p>

2 个答案:

答案 0 :(得分:1)

CheckBox Chk = (CheckBox) findViewById(R.id.checkbox);
                    Chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {
                            if (isChecked) {
                             Toast.makeText(context, "Screen is turned ON. Broadcast Received", Toast.LENGTH_LONG).show();  
                            }
                        }
                    });

试试这个可以正常使用

答案 1 :(得分:0)

在onCreate下

   mSavePassword = (CheckBox) findViewById(R.id.checkBox1);

    mSavePassword.setOnClickListener(this);

在Onclick

    @Override
  public void onClick(View view) 
   {

    switch(view.getId())
    {

    case R.id.checkBox1:

    Toast.makeText(context, "Screen is turned ON. Broadcast Received", Toast.LENGTH_LONG).show();
        break;
    }
   }