Android 如何仅在选中复选框时显示toast Measage?
我的Android 如何仅在选中复选框时显示toast Measage?,android,android-checkbox,android-toast,Android,Android Checkbox,Android Toast,我的活动\u main.xml中有一个复选框 我有以下代码: public class UpdateService extends Service { @Override public void onCreate() { super.onCreate(); IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); filter.addAction(Intent
活动\u 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;
}
}
我只想在选中复选框时显示Toast
通知,否则不应显示该通知
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();
}
}
});
试试这个,在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;
}
}
选中复选框后,将值存储在SharedReference中,然后选中此处。将您的值与存储的值进行比较。如果是,则显示toast消息。是否要访问服务中的复选框?是。我想访问中的复选框service@MissNoob请尝试我的答案。您不能这样做,因为复选框是GUI组件,仅在活动中可用。获取以下错误findViewById:类型UpdateService的方法findViewById(int)未定义。请告诉我正确的方法。我想不出来。