Android 为什么setChecked on开关不工作?
我正在Android 为什么setChecked on开关不工作?,android,Android,我正在onSaveInstanceState中保存开关切换的状态,并在onRestoreInstanceState中检索它。每当我旋转屏幕时,我都想恢复开关的状态。若我将开关设置为ON,它将保存true,但在旋转屏幕上,活动将重新创建,所以我使用 switch.setChecked(isChecked); isChecked来自savedInstanceState。但问题是setChecked(true)不起作用 MainActivity.java package com.chinmay.ca
onSaveInstanceState
中保存开关切换的状态,并在onRestoreInstanceState
中检索它。每当我旋转屏幕时,我都想恢复开关的状态。若我将开关设置为ON,它将保存true,但在旋转屏幕上,活动将重新创建,所以我使用
switch.setChecked(isChecked);
isChecked来自savedInstanceState。但问题是setChecked(true)
不起作用
MainActivity.java
package com.chinmay.callblocker;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
private Switch mSwitch;
private AudioManager mAudio;
private TextView extraTxt;
CustomListAdapter adapter;
String[] itemname ={
"Activate",
"Message",
"Global",
"FireFox",
"UC Browser",
"Android Folder"
};
Integer[] imgid={
R.drawable.pic1,
R.drawable.message,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5,
R.drawable.pic6
};
String[] textDescription = {
"Turn on to block calls",
"Set a custom message",
"Description Global",
"Description FireFox",
"Description UC Browser",
"Description Android Folder"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new CustomListAdapter(this, itemname, imgid, textDescription);
setListAdapter(adapter);
mAudio = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
View inflatedView = getLayoutInflater().inflate(R.layout.mylist, null);
mSwitch = (Switch) inflatedView.findViewById(R.id.switch1);
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor e = settings.edit();
e.putBoolean("switch", false);
e.commit();
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, final int position, long l) {
if(position == 1) {
alert.setTitle("Message");
alert.setMessage("This message will be sent when call blocking is on");
// Set an EditText view to get user input
final EditText input = new EditText(getBaseContext());
input.setText(textDescription[position]);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
textDescription[1] = value;
adapter.notifyDataSetChanged();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
}
});
}
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString("current_message", textDescription[1]);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
boolean isChecked = settings.getBoolean("switch", false);
//Toast.makeText(this, "Current state of switch is "+ isChecked, Toast.LENGTH_SHORT).show();
savedInstanceState.putBoolean("switchChecked", isChecked);
super.onSaveInstanceState(savedInstanceState);
}
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
textDescription[1] = savedInstanceState.getString("current_message");
adapter.notifyDataSetChanged();
boolean switchChecked = savedInstanceState.getBoolean("switchChecked");
//Toast.makeText(this, "Current state of switch is "+ switchChecked, Toast.LENGTH_SHORT).show();
mSwitch.setChecked(switchChecked);
}
public void onListItemClick(ListView lv ,View view,int position,int imgid) {
/*String selected_item = "List View";
Toast.makeText(this, selected_item, Toast.LENGTH_SHORT).show();*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_logs) {
return true;
} else if(id == R.id.action_help) {
return true;
} else if(id == R.id.action_about) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
package com.chinmay.callblocker;
导入android.app.AlertDialog;
导入android.app.ListActivity;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.SharedReferences;
导入android.media.AudioManager;
导入android.os.Bundle;
导入android.preference.PreferenceManager;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.CompoundButton;
导入android.widget.EditText;
导入android.widget.ListView;
导入android.widget.Switch;
导入android.widget.TextView;
导入android.widget.Toast;
公共类MainActivity扩展了ListActivity{
专用交换开关;
私人音频经理莫迪奥;
私有文本视图extraTxt;
自定义列表适配器;
字符串[]itemname={
“激活”,
“信息”,
“全球”,
“火狐”,
“UC浏览器”,
“Android文件夹”
};
整数[]imgid={
R.drawable.pic1,
R.drawable.message,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5,
R.drawable.pic6
};
字符串[]文本描述={
“打开以阻止呼叫”,
“设置自定义消息”,
“全球描述”,
“描述火狐”,
“说明UC浏览器”,
“描述Android文件夹”
};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter=新的CustomListAdapter(this,itemname,imgid,textDescription);
setListAdapter(适配器);
mAudio=(AudioManager)getBaseContext().getSystemService(Context.AUDIO_服务);
View inflatedView=GetLayoutFlater().inflate(R.layout.mylist,null);
mSwitch=(开关)充气视图.findViewById(R.id.switch1);
final AlertDialog.Builder alert=新建AlertDialog.Builder(此);
SharedReferences设置=PreferenceManager.GetDefaultSharedReferences(此);
final SharedPreferences.Editor e=settings.edit();
e、 putBoolean(“开关”,false);
e、 提交();
getListView().setOnItemClickListener(新的AdapterView.OnItemClickListener()){
@凌驾
公共虚线单击(AdapterView AdapterView,视图视图,最终整型位置,长l){
如果(位置==1){
警报。设置标题(“消息”);
alert.setMessage(“此消息将在呼叫阻止打开时发送”);
//设置EditText视图以获取用户输入
最终编辑文本输入=新编辑文本(getBaseContext());
input.setText(textDescription[位置]);
alert.setView(输入);
alert.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(对话框接口对话框,int whichButton){
字符串值=input.getText().toString();
textDescription[1]=值;
adapter.notifyDataSetChanged();
}
});
alert.setNegativeButton(“取消”,新建DialogInterface.OnClickListener()){
public void onClick(对话框接口对话框,int whichButton){
//取消了。
}
});
alert.show();
}
}
});
}
SaveInstanceState上的公共无效(Bundle savedInstanceState){
savedInstanceState.putString(“当前_消息”,textDescription[1]);
SharedReferences设置=PreferenceManager.GetDefaultSharedReferences(此);
boolean isChecked=settings.getBoolean(“开关”,false);
//Toast.makeText(此“开关的当前状态为”+isChecked,Toast.LENGTH_SHORT).show();
savedInstanceState.putBoolean(“开关选中”,已选中);
super.onSaveInstanceState(savedInstanceState);
}
RestoreInstanceState上的公共无效(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
textDescription[1]=savedInstanceState.getString(“当前_消息”);
adapter.notifyDataSetChanged();
boolean switchChecked=savedInstanceState.getBoolean(“switchChecked”);
//Toast.makeText(该“开关的当前状态为”+switchChecked,Toast.LENGTH_SHORT).show();
mSwitch.setChecked(开关检查);
}
public void onListItemClick(列表视图lv、视图视图、int位置、int imgid){
/*所选字符串\u item=“列表视图”;
Toast.makeText(此,所选项目,Toast.LENGTH\u SHORT).show()*/
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action_日志){
返回true;
}else if(id==R.id.action\u help){
返回true;
}else if(id==R.id.action\u about){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
}
mylist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#33CC33" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="21dp"
android:layout_marginBottom="21dp" />
</LinearLayout>
问题是“开关”键的值总是错误的。您在此处仅输入
false
值:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor e = settings.edit();
e.putBoolean("switch", false);
e.commit();
然后再也不会
mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor e = settings.edit();
e.putBoolean("switch", isChecked);
e.commit();
}
});