完成效果:选择选项后退出,界面记住选择选项。
1.利用SharePreferences方法记住选择的选项
主布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingTop="16dp"
tools:context="com.example.administrator.homework8_6.ScrollViewActivity">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取"
android:id="@+id/read"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置"
android:layout_marginLeft="50dp"
android:textSize="30sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="完成"
android:layout_gravity="center"
android:layout_marginLeft="50dp"
android:id="@+id/save"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#d9d3d3"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="列表显示摘要"
android:textSize="20sp"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="170dp"
android:id="@+id/cb1"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#d9d3d3"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="字体大小"
android:textSize="20sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中"
android:layout_marginLeft="220dp"
android:id="@+id/word"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=">"
android:textSize="20sp"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#d9d3d3"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="列表页评论"
android:textSize="20sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="不限"
android:layout_marginLeft="190dp"
android:id="@+id/pinglun"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=">"
android:textSize="20sp"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#d9d3d3"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2G/3G网络流量"
android:textSize="20sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="较省流量(智能下图)"
android:layout_gravity="center"
android:layout_marginLeft="50dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=">"
android:textSize="20sp"
android:layout_marginLeft="1dp"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#d9d3d3"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清理缓存"
android:textSize="20sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="当前缓存:2.48MB"
android:layout_gravity="center"
android:layout_marginLeft="120dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=">"
android:textSize="20sp"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#d9d3d3"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="推送通知"
android:textSize="20sp"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_marginLeft="210dp"
android:id="@+id/cb2"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#d9d3d3"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="互动插件"
android:textSize="20sp"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_marginLeft="210dp"
android:id="@+id/cb3"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#d9d3d3"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动优化阅读"
android:textSize="20sp"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_marginLeft="170dp"
android:id="@+id/cb4"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#d9d3d3"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="收藏时转发"
android:textSize="20sp"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="190dp"
android:id="@+id/cb5"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#d9d3d3"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点赞时转发"
android:textSize="20sp"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="190dp"
android:id="@+id/cb6"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#d9d3d3"
/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
布局效果:
代码:
代码:2.将选择的选项写入到Sd卡中package com.example.administrator.jreduch09.Class; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import com.example.administrator.jreduch09.R; public class TestActivity extends AppCompatActivity { private Button save; private CheckBox cb1,cb2,cb3,cb4,cb5,cb6; private SharedPreferences sp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.homework); save= (Button) findViewById(R.id.save); cb1= (CheckBox) findViewById(R.id.cb1); cb2= (CheckBox) findViewById(R.id.cb2); cb3= (CheckBox) findViewById(R.id.cb3); cb4= (CheckBox) findViewById(R.id.cb4); cb5= (CheckBox) findViewById(R.id.cb5); cb6= (CheckBox) findViewById(R.id.cb6); op(); sp=getSharedPreferences("set.txt",MODE_APPEND); save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences.Editor editor=sp.edit(); editor.putBoolean("1",cb1.isChecked()); editor.putBoolean("2",cb2.isChecked()); editor.putBoolean("3",cb3.isChecked()); editor.putBoolean("4",cb4.isChecked()); editor.putBoolean("5",cb5.isChecked()); editor.putBoolean("6",cb6.isChecked()); editor.commit(); } }); } public void op(){ Boolean c1=sp.getBoolean("1",false); Boolean c2=sp.getBoolean("2", false); Boolean c3=sp.getBoolean("3",false); Boolean c4=sp.getBoolean("4", false); Boolean c5=sp.getBoolean("5",false); Boolean c6=sp.getBoolean("6", false); cb1.setChecked(c1); cb2.setChecked(c2); cb3.setChecked(c3); cb4.setChecked(c4); cb5.setChecked(c5); cb6.setChecked(c6); } }
package com.example.administrator.jreduch09;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import com.example.administrator.jreduch09.Class.shezhi;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
public class SettingActivity extends AppCompatActivity {
private Button save;
private CheckBox cb1,cb2,cb3,cb4,cb5,cb6;
private TextView word;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homework);
save= (Button) findViewById(R.id.save);
cb1= (CheckBox) findViewById(R.id.cb1);
cb2= (CheckBox) findViewById(R.id.cb2);
cb3= (CheckBox) findViewById(R.id.cb3);
cb4= (CheckBox) findViewById(R.id.cb4);
cb5= (CheckBox) findViewById(R.id.cb5);
cb6= (CheckBox) findViewById(R.id.cb6);
word= (TextView) findViewById(R.id.word);
readFile();
word.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder=new AlertDialog.Builder(SettingActivity.this);
builder.setTitle("选择字体大小");
builder.setIcon(R.mipmap.ic_launcher);
final String [] items={"大","中","小"};
builder.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
Toast.makeText(getBaseContext(), items[i].toString(),
Toast.LENGTH_SHORT).show();
word.setText(items[i].toString());
dialog.dismiss();
}
});
AlertDialog alertDialog=builder.create();
alertDialog.show();
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
shezhi sz =new shezhi();
sz.setCb1(cb1.isChecked());
sz.setCb2(cb2.isChecked());
sz.setCb3(cb3.isChecked());
sz.setCb4(cb4.isChecked());
sz.setCb5(cb5.isChecked());
sz.setCb6(cb6.isChecked());
sz.setWord(word.getText().toString());
String state= Environment.getExternalStorageState();
if(!state.equals(Environment.MEDIA_MOUNTED)){
return;
}
File root=Environment.getExternalStorageDirectory();
FileOutputStream fos=null;
ObjectOutputStream oos=null;
try {
fos=new FileOutputStream(root+"/shezhi.txt");
oos=new ObjectOutputStream(fos);
oos.writeObject(sz);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(oos!=null){
try {
oos.flush();
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
}
public void readFile(){
File root=Environment.getExternalStorageDirectory();
FileInputStream fis=null;
ObjectInputStream ois=null;
try {
fis=new FileInputStream(root+"/shezhi.txt");
ois=new ObjectInputStream(fis);
shezhi sz= (shezhi) ois.readObject();
cb1.setChecked(sz.isCb1());
cb2.setChecked(sz.isCb2());
cb3.setChecked(sz.isCb3());
cb4.setChecked(sz.isCb4());
cb5.setChecked(sz.isCb5());
cb6.setChecked(sz.isCb6());
word.setText(sz.getWord());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (StreamCorruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
if(ois!=null){
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
效果展示: