1.±£´æÊý¾Ý˽ÓÐÎļþ
<LinearLayout 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:orientation="vertical" tools:context=".MainActivity" >
<ImageView
android:background="#0090CA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/qq"/>
<!--ºÅÂë -->
<EditText
android:id="@+id/et_qqNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="ÇëÊäÈëqqºÅÂë"/>
<!--ÃÜÂë -->
<EditText
android:id="@+id/et_qqPwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="ÇëÊäÈëqqÃÜÂë"/>
<!--¼ÇסÃÜÂ븴ѡ¿ò -->
<CheckBox
android:id="@+id/cb_remember"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="¼ÇסÃÜÂë"/>
<!--µÇ¼°´¼ü -->
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#09A3DC"
android:textSize="20sp"
android:textColor="#fff"
android:text="怬"/>
</LinearLayout> |
Activity
/** * QQ µÇ¼ ±£´æÃÜÂ뵽˽ÓÐÎļþ * ²½Öè * 1.»ñÈ¡ÊäÈëµÄÓû§ÃûÓëÃÜÂë * 2.ÅжÏÊÇ·ñ Ϊ¿Õ,Ϊ¿Õ¾Í¸øÓû§ÌáʾToast * 3.±£´æÓû§ÃûÓëÃÜÂëµ½Îļþ * 4.Êý¾Ý»ØÏÔ * * @author Áõéª * * 2016-2-18ÏÂÎç12:39:54 */ public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = "MainActivity";
/*
* QQºÅÂë
*/
private EditText et_qqNumber;
/*
* QQÃÜÂë
*/
private EditText et_qqPwd;
/*
* ¼ÇסÃÜÂë,¸´Ñ¡¿ò
*/
private CheckBox cb_remember;
/*
* µÇ¼°´¼ü
*/
private Button btn_login;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* ³õʼ»¯
*/
et_qqNumber = (EditText) findViewById(R.id.et_qqNumber);
et_qqPwd = (EditText) findViewById(R.id.et_qqPwd);
cb_remember = (CheckBox) findViewById(R.id.cb_remember);
btn_login = (Button) findViewById(R.id.btn_login);
/*
* ΪµÇ¼°´¼üÉèÖõ¥»÷ʼþ
*/
btn_login.setOnClickListener(this);
/*
* »ØÏÔʾÊý¾Ý
*/
Map<String, String> map=QQLoginUtils.getUser(this);
if(map!=null){
et_qqNumber.setText(map.get("username"));
et_qqPwd.setText(map.get("password"));
String isChecked = map.get("isChecked");
if("true".equals(isChecked)){
cb_remember.setChecked(true);
}else{
cb_remember.setChecked(false);
}
}
}
/**
* µ¥»÷ʼþ,¼àÌýÆ÷
*/
@Override
public void onClick(View v) {
// ÅжÏID
switch (v.getId()) {
case R.id.btn_login:
// Ö´ÐÐÏàÓ¦µÄ·½·¨
qqLogin();
break;
default:
break;
}
}
/**
* µÇ¼·½·¨
*/
public void qqLogin() {
/*
* »ñÈ¡Óû§ÃûÓëÃÜÂë
*/
String qq = et_qqNumber.getText().toString().trim();
String pwd = et_qqPwd.getText().toString().trim();
// ÅжÏÊÇ·ñΪ¿Õ
if (TextUtils.isEmpty(qq) || TextUtils.isEmpty(pwd))
{
Toast.makeText(this, "Ç×,qqºÅÂë ÓëÃÜÂë²»ÄÜΪ¿Õ!",
Toast.LENGTH_SHORT).show();
return;
}
/*
* ÅÐ¶Ï ÊÇ·ñÒª±£´æ
*/
if (cb_remember.isChecked()) {
Log.i(TAG, "±£´æÓû§ÃûÓëÃÜÂë");
// Òª±£´æ
boolean flag = QQLoginUtils.saveUser(this, qq,
pwd,cb_remember.isChecked());
// ÅÐ¶Ï ÊÇ·ñ±£´æ³É¹¦
if (flag) {
Toast.makeText(this, "±£´æ³É¹¦", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "±£´æÊ§°Ü", Toast.LENGTH_SHORT).show();
}
} else {
//²»Òª±£´æ
Log.i(TAG, "²»±£´æÓû§ÃûÓëÃÜÂë");
boolean flag = QQLoginUtils.saveUser(this, "",
"",cb_remember.isChecked());
// ÅÐ¶Ï ÊÇ·ñ±£´æ³É¹¦
if (flag) {
Toast.makeText(this, "±£´æ³É¹¦", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "±£´æÊ§°Ü", Toast.LENGTH_SHORT).show();
}
}
}
} |
Utils
/** * ±£´æÓû§ÃûÃÜÂ빤¾ßÀà * @author Áõéª * * 2016-2-18ÏÂÎç12:50:55 */ public class QQLoginUtils {
/**
*
* @param context ÉÏÏÂÎÄ
* @param name Óû§Ãû
* @param password ÃÜÂë
* @param isChecked ¼Ç¼ÃÜÂë״̬
* @return ÊÇ·ñ±£´æ³É¹¦
*/
public static boolean saveUser(Context context,
String name, String password, boolean isChecked)
{
OutputStream out=null;
try {
File file = new File(context.getFilesDir(), "user.txt");
out = new FileOutputStream(file);
String data = name +"#"+password+"#"+isChecked;
out.write(data.getBytes());
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static Map<String, String> getUser(Context
context) {
File file = new File(context.getFilesDir(), "user.txt");
/*
* ÅжÏÎļþÊÇ·ñ´æÔÚ
*/
if(!file.exists()){
return null;
}
/*
* »ñÈ¡Îļþ
*/
Map<String,String> map= new HashMap<String,
String>();
BufferedReader br=null;
try {
br = new BufferedReader(new InputStreamReader(new
FileInputStream(file)));
String data = br.readLine();
String[] split = data.split("#");
map.put("username", split[0]);
map.put("password", split[1]);
map.put("isChecked", split[2]);
//·µ»Ø½á¹û
br.close();
return map;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//
return null;
}
} |
2.±£´æÊý¾ÝÒªSD¿¨
¸ü¸Ä¹¤¾ßÀà
/** * ±£´æÓû§ÃûÃÜÂ빤¾ßÀà * @author Áõéª * * 2016-2-18ÏÂÎç12:50:55 */ public class QQLoginUtils {
/**
*
* @param context ÉÏÏÂÎÄ
* @param name Óû§Ãû
* @param password ÃÜÂë
* @param isChecked
* @return ÊÇ·ñ±£´æ³É¹¦
*/
public static boolean saveUser(Context context,
String name, String password, boolean isChecked)
{
/*
* ÅжÏSD¿¨ÊÇ·ñÕý³£
*/
String state = Environment.getExternalStorageState();
if(!Environment.MEDIA_MOUNTED.equals(state)){
Toast.makeText(context, "SD¿¨Ã»ÓвåÈë", Toast.LENGTH_SHORT).show();
return false;
}
OutputStream out=null;
try {
File file = new File(Environment.getExternalStorageDirectory(),
"user.txt");
out = new FileOutputStream(file);
String data = name +"#"+password+"#"+isChecked;
out.write(data.getBytes());
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static Map<String, String> getUser(Context
context) {
/*
* ÅжÏSD¿¨ÊÇ·ñÕý³£
*/
String state = Environment.getExternalStorageState();
if(!Environment.MEDIA_MOUNTED.equals(state)){
Toast.makeText(context, "SD¿¨Ã»ÓвåÈë", Toast.LENGTH_SHORT).show();
return null;
}
File file = new File(Environment.getExternalStorageDirectory(),
"user.txt");
/*
* ÅжÏÎļþÊÇ·ñ´æÔÚ
*/
if(!file.exists()){
return null;
}
/*
* »ñÈ¡Îļþ
*/
Map<String,String> map= new HashMap<String,
String>();
BufferedReader br=null;
try {
br = new BufferedReader(new InputStreamReader(new
FileInputStream(file)));
String data = br.readLine();
String[] split = data.split("#");
map.put("username", split[0]);
map.put("password", split[1]);
map.put("isChecked", split[2]);
//·µ»Ø½á¹û
br.close();
return map;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//
return null;
}
} |
Ìí¼ÓдÈëSD¿¨µÄȨÏÞ
ÔÚManifest.xmlÖÐ
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> |
3.±£´æÊý¾Ýµ½SharedPreferences
²¼¾ÖÎļþ²»±ä
/** * QQ µÇ¼ ±£´æÃÜÂëµ½SharedPreferences * ²½Öè 1.»ñÈ¡ÊäÈëµÄÓû§ÃûÓëÃÜÂë * 2.ÅжÏÊÇ·ñ Ϊ¿Õ,Ϊ¿Õ¾Í¸øÓû§ÌáʾToast * 3.±£´æÓû§ÃûÓëÃÜÂëµ½Îļþ * * @author Áõéª * * 2016-2-18ÏÂÎç12:39:54 */ public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = "MainActivity";
/*
* QQºÅÂë
*/
private EditText et_qqNumber;
/*
* QQÃÜÂë
*/
private EditText et_qqPwd;
/*
* ¼ÇסÃÜÂë,¸´Ñ¡¿ò
*/
private CheckBox cb_remember;
/*
* µÇ¼°´¼ü
*/
private Button btn_login;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* ³õʼ»¯
*/
et_qqNumber = (EditText) findViewById(R.id.et_qqNumber);
et_qqPwd = (EditText) findViewById(R.id.et_qqPwd);
cb_remember = (CheckBox) findViewById(R.id.cb_remember);
btn_login = (Button) findViewById(R.id.btn_login);
/*
* ΪµÇ¼°´¼üÉèÖõ¥»÷ʼþ
*/
btn_login.setOnClickListener(this);
/*
* »ØÏÔʾÊý¾Ý
*/
Map<String, Object> map=QQLoginUtils.getUser(this);
if(map!=null){
et_qqNumber.setText(map.get("username").toString());
et_qqPwd.setText(map.get("password").toString());
boolean isChecked = (Boolean) map.get("isChecked");
cb_remember.setChecked(isChecked);
}
}
/**
* µ¥»÷ʼþ,¼àÌýÆ÷
*/
@Override
public void onClick(View v) {
// ÅжÏID
switch (v.getId()) {
case R.id.btn_login:
// Ö´ÐÐÏàÓ¦µÄ·½·¨
qqLogin();
break;
}
}
/**
* µÇ¼·½·¨
*/
public void qqLogin() {
/*
* »ñÈ¡Óû§ÃûÓëÃÜÂë
*/
String qq = et_qqNumber.getText().toString().trim();
String pwd = et_qqPwd.getText().toString().trim();
// ÅжÏÊÇ·ñΪ¿Õ
if (TextUtils.isEmpty(qq) || TextUtils.isEmpty(pwd))
{
Toast.makeText(this, "Ç×,qqºÅÂë ÓëÃÜÂë²»ÄÜΪ¿Õ!",
Toast.LENGTH_SHORT).show();
return;
}
/*
* ÅÐ¶Ï ÊÇ·ñÒª±£´æ
*/
if (cb_remember.isChecked()) {
Log.i(TAG, "±£´æÓû§ÃûÓëÃÜÂë");
// Òª±£´æ
boolean flag = QQLoginUtils.saveUser(this, qq,
pwd,cb_remember.isChecked());
// ÅÐ¶Ï ÊÇ·ñ±£´æ³É¹¦
if (flag) {
Toast.makeText(this, "±£´æ³É¹¦", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "±£´æÊ§°Ü", Toast.LENGTH_SHORT).show();
}
} else {
//²»Òª±£´æ
Log.i(TAG, "²»±£´æÓû§ÃûÓëÃÜÂë");
boolean flag = QQLoginUtils.saveUser(this, null,
null,cb_remember.isChecked());
// ÅÐ¶Ï ÊÇ·ñ±£´æ³É¹¦
if (flag) {
Toast.makeText(this, "±£´æ³É¹¦", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "±£´æÊ§°Ü", Toast.LENGTH_SHORT).show();
}
}
}
} |
¹¤¾ßÀà
/** * ±£´æÓû§ÃûÃÜÂ빤¾ßÀà * @author Áõéª * * 2016-2-18ÏÂÎç12:50:55 */ public class QQLoginUtils {
/**
*
* @param context ÉÏÏÂÎÄ
* @param name Óû§Ãû
* @param password ÃÜÂë
* @param isChecked
* @return ÊÇ·ñ±£´æ³É¹¦
*/
public static boolean saveUser(Context context,
String name, String password, boolean isChecked)
{
SharedPreferences preferences = context.getSharedPreferences("config",
Context.MODE_PRIVATE);
Editor editor = preferences.edit();
editor.putString("username", name);
editor.putString("password", password);
editor.putBoolean("isChecked", isChecked);
editor.commit();
return true;
}
/**
* È¡³öÊý¾Ý
* @param context
* @return
*/
public static Map<String, Object> getUser(Context
context) {
Map<String, Object> map= new HashMap<String,
Object>();
SharedPreferences preferences = context.getSharedPreferences("config",
Context.MODE_PRIVATE);
String username = preferences.getString("username",
"");
String password = preferences.getString("password",
"");
boolean isChecked = preferences.getBoolean("isChecked",
false);
map.put("username", username);
map.put("password", password);
map.put("isChecked", isChecked);
return map;
}
} |
4.±£´æÊý¾Ýµ½XMLÎļþ
<LinearLayout 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:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="ѧÉúÐÅÏ¢¹ÜÀíϵͳ"
android:textColor="#ff0000"
android:textSize="28sp" />
<!-- ѧÉúÐÕÃû -->
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="ÇëÊäÈëѧÉúÐÕÃû" />
<!-- ÐÔ±ð -->
<RadioGroup
android:id="@+id/rg_gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rb_man"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:text="ÄÐ" />
<RadioButton
android:id="@+id/rb_felman"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Å®" />
</RadioGroup>
<Button
android:id="@+id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#09A3DC"
android:text="±£´æ"
android:textColor="#fff" />
<Button
android:id="@+id/btn_query"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#09A3DC"
android:text="²éѯ"
android:textColor="#fff" />
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout> |
/** * ѧÉúÐÅÏ¢¹ÜÀíϵͳ ±£´æÑ§ÉúÐÅÏ¢µ½XMLÎļþ serializer ²½Öè: 1.»ñÈ¡Óû§ÊäÈëµÄÓû§Ãû 2.ÅжÏÊäÈëÊÇ·ñΪ¿Õ, Ϊ¿Õ¸øÓû§Ìáʾ,Toast 3.ʹÓà serializer,Éú³ÉXML±£´æ ²éѯ 1.»ñÈ¡Óû§ÊäÈëµÄÐÕÃû 2.²éÕÒÎļþÊÇ·ñ´æÔÚ 3.´æÔھͽâÎöpull½âÎö,²¢ÏÔʾ,²»´æÔÚ¾ÍÌáʾ * @author Áõéª * * 2016-2-18ÏÂÎç7:50:09 */ public class MainActivity extends Activity implements OnClickListener{
/*
* ѧÉúÐÕÃû
*/
private EditText et_name;
/*
* ѧÉúÐÔ±ð
*/
private RadioGroup rg_gender;
/*
* ±£´æ
*/
private Button btn_save;
/*
* ²éѯ
*/
private Button btn_query;
/*
* ÏÔʾ²éѯ½á¹û
*/
private TextView tv_result;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = (EditText) findViewById(R.id.et_name);
rg_gender = (RadioGroup) findViewById(R.id.rg_gender);
btn_save = (Button) findViewById(R.id.btn_save);
btn_query = (Button) findViewById(R.id.btn_query);
tv_result = (TextView) findViewById(R.id.tv_result);
/*
* ÉèÖõ¥»÷ʼþ
*/
btn_save.setOnClickListener(this);
btn_query.setOnClickListener(this);
}
/**
* µ¥»÷ʼþ¼àÌýÆ÷
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_save:
//±£´æ
save();
break;
case R.id.btn_query:
//²éѯ
query();
break;
}
}
/**
* ²éѯ
*/
private void query() {
//»ñÈ¡Óû§ÊäÈëµÄÓû§Ãû
String name = et_name.getText().toString().trim();
//ÅжÏ
if(TextUtils.isEmpty(name)){
Toast.makeText(this, "ѧÉúÐÕÃû²»ÄÜΪ¿Õ", Toast.LENGTH_SHORT).show();
return ;
}
Map<String, String> map = StudentUtils.getStudent(this,name);
//tv_result;
if(map!=null){
String data="ÐÕÃû:"+map.get("name")+",ÐÔ±ð:"+map.get("gender");
tv_result.setText(data);
}
}
/**
* ±£´æ
*/
private void save() {
//»ñÈ¡Óû§ÊäÈëµÄÓû§Ãû
String name = et_name.getText().toString().trim();
//ÅжÏ
if(TextUtils.isEmpty(name)){
Toast.makeText(this, "ѧÉúÐÕÃû²»ÄÜΪ¿Õ", Toast.LENGTH_SHORT).show();
return ;
}
//ÅжÏÐÔ±ð
String gender="ÄÐ";
switch (rg_gender.getCheckedRadioButtonId()) {
case R.id.rb_man:
RadioButton rb_man = (RadioButton) findViewById(R.id.rb_man);
gender=rb_man.getText().toString().trim();
break;
case R.id.rb_felman:
RadioButton rb_felman = (RadioButton) findViewById(R.id.rb_felman);
gender=rb_felman.getText().toString().trim();
break;
}
/*
* ¿ªÊ¼±£´æÎļþ serializer
*/
boolean flag =StudentUtils.save(this,name,gender);
if(flag){
Toast.makeText(this, "±£´æ³É¹¦", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "±£´æÊ§°Ü", Toast.LENGTH_SHORT).show();
}
}
} |
¹¤¾ßÀà
/** * ѧÉú±£´æ,²éѯµÄ¹¤¾ßÀà * * @author Áõéª * * 2016-2-18ÏÂÎç8:09:29 */ public class StudentUtils {
public static boolean save(Context context,
String name, String gender) {
File file = new File(context.getFilesDir(),
name + ".xml");
try {
OutputStream out = new FileOutputStream(file);
// »ñÈ¡XMLÉú³É¹¤¾ßserializer
XmlSerializer serializer = Xml.newSerializer();
// µ÷ÖÃÊä³ö±àÂë
serializer.setOutput(out, "UTF-8");
// ÉèÖÃXMLÍ·
serializer.startDocument("UTF-8", true);
serializer.startTag(null, "student");
serializer.startTag(null, "name");
serializer.text(name);
serializer.endTag(null, "name");
serializer.startTag(null, "gender");
serializer.text(gender);
serializer.endTag(null, "gender");
serializer.endTag(null, "student");
serializer.endDocument();
// ¹Ø±ÕÁ÷
out.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/*
* ²éѯ
*/
public static Map<String, String> getStudent(Context
context, String name) {
File file = new File(context.getFilesDir(),
name + ".xml");
if (!file.exists()) {
Toast.makeText(context, "ѧÉúÐÅÏ¢²»´æÔÚ", Toast.LENGTH_SHORT).show();
return null;
}
Map<String, String> map = new HashMap<String,
String>();
try {
InputStream in = new FileInputStream(file);
// ʹÓÃpull½âÎö
XmlPullParser pullParser = Xml.newPullParser();
// ¶ÁÈ¡±àÂë
pullParser.setInput(in, "UTF-8");
// ʼþ
int eventType = pullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
// ÅжÏÊDz»ÊÇ¿ªÊ¼±êÇ©
if (eventType == XmlPullParser.START_TAG) {
if ("name".equals(pullParser.getName()))
{
String stuName = pullParser.nextText();
map.put("name", stuName);
} else if ("gender".equals(pullParser.getName()))
{
String gender = pullParser.nextText();
map.put("gender", gender);
}
}
// ÏòÏÂÒÆ¶¯Ö¸Õë
eventType = pullParser.next();
}
in.close();
return map;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
} |
5.±£´æ¸÷ÖÖȨÏÞµÄÎļþ
<LinearLayout 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:orientation="vertical" tools:context=".MainActivity" >
<ImageView
android:background="#0090CA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/qq"/>
<!--ºÅÂë -->
<EditText
android:id="@+id/et_qqNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="ÇëÊäÈëqqºÅÂë"/>
<!--ÃÜÂë -->
<EditText
android:id="@+id/et_qqPwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="ÇëÊäÈëqqÃÜÂë"/>
<!--¼ÇסÃÜÂ븴ѡ¿ò -->
<!-- <CheckBox
android:id="@+id/cb_remember"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="¼ÇסÃÜÂë"/> -->
<RadioGroup
android:id="@+id/rg_premission"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_private"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="˽ÓÐ"/>
<RadioButton
android:id="@+id/rb_readable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¿É¶Á"/>
<RadioButton
android:id="@+id/rb_writable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¿Éд"/>
<RadioButton
android:id="@+id/rb_public"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¹«ÓÐ"/>
</RadioGroup>
<!--µÇ¼°´¼ü -->
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#09A3DC"
android:textSize="20sp"
android:textColor="#fff"
android:text="怬"/>
</LinearLayout> |
/** * QQ µÇ¼ ±£´æÃÜÂ뵽˽ÓÐÎļþ * ²½Öè 1.»ñÈ¡ÊäÈëµÄÓû§ÃûÓëÃÜÂë * 2.ÅжÏÊÇ·ñ Ϊ¿Õ,Ϊ¿Õ¾Í¸øÓû§ÌáʾToast * 3.±£´æÓû§ÃûÓëÃÜÂëµ½Îļþ * * @author Áõéª * * 2016-2-18ÏÂÎç12:39:54 */ public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = "MainActivity";
/*
* QQºÅÂë
*/
private EditText et_qqNumber;
/*
* QQÃÜÂë
*/
private EditText et_qqPwd;
/*
* ¼ÇסÃÜÂë,¸´Ñ¡¿ò
*/
//private CheckBox cb_remember;
/*
* µÇ¼°´¼ü
*/
private Button btn_login;
private RadioGroup rg_premission;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* ³õʼ»¯
*/
et_qqNumber = (EditText) findViewById(R.id.et_qqNumber);
et_qqPwd = (EditText) findViewById(R.id.et_qqPwd);
// cb_remember = (CheckBox) findViewById(R.id.cb_remember);
btn_login = (Button) findViewById(R.id.btn_login);
rg_premission = (RadioGroup) findViewById(R.id.rg_premission);
/*
* ΪµÇ¼°´¼üÉèÖõ¥»÷ʼþ
*/
btn_login.setOnClickListener(this);
/*
* »ØÏÔʾÊý¾Ý
*/
Map<String, String> map=QQLoginUtils.getUser(this);
if(map!=null){
et_qqNumber.setText(map.get("username"));
et_qqPwd.setText(map.get("password"));
}
}
/**
* µ¥»÷ʼþ,¼àÌýÆ÷
*/
@Override
public void onClick(View v) {
// ÅжÏID
switch (v.getId()) {
case R.id.btn_login:
// Ö´ÐÐÏàÓ¦µÄ·½·¨
qqLogin();
break;
default:
break;
}
}
/**
* µÇ¼·½·¨
*/
public void qqLogin() {
/*
* »ñÈ¡Óû§ÃûÓëÃÜÂë
*/
String qq = et_qqNumber.getText().toString().trim();
String pwd = et_qqPwd.getText().toString().trim();
// ÅжÏÊÇ·ñΪ¿Õ
if (TextUtils.isEmpty(qq) || TextUtils.isEmpty(pwd))
{
Toast.makeText(this, "Ç×,qqºÅÂë ÓëÃÜÂë²»ÄÜΪ¿Õ!",
Toast.LENGTH_SHORT).show();
return;
}
/*
* ÅÐ¶Ï Òª±£´æµÄÀàÐÍ
*/
int radioButtonId = rg_premission.getCheckedRadioButtonId();
boolean flag = false ;
switch (radioButtonId) {
case R.id.rb_private:
//˽ÓÐ
flag= QQLoginUtils.saveUser(this, qq, pwd,1);
break;
case R.id.rb_readable:
//Ö»¶Á
flag= QQLoginUtils.saveUser(this, qq, pwd,2);
break;
case R.id.rb_writable:
//¿Éд
flag= QQLoginUtils.saveUser(this, qq, pwd,3);
break;
case R.id.rb_public:
//¿É¶Á¿Éд
flag= QQLoginUtils.saveUser(this, qq, pwd,4);
break;
}
// Òª±£´æ
// ÅÐ¶Ï ÊÇ·ñ±£´æ³É¹¦
if (flag) {
Toast.makeText(this, "±£´æ³É¹¦", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "±£´æÊ§°Ü", Toast.LENGTH_SHORT).show();
}
}
} |
¹¤¾ßÀà
/** * ±£´æÓû§ÃûÃÜÂ빤¾ßÀà * @author Áõéª * * 2016-2-18ÏÂÎç12:50:55 */ public class QQLoginUtils {
/**
*
* @param context ÉÏÏÂÎÄ
* @param name Óû§Ãû
* @param password ÃÜÂë
* @param mode ģʽ 1,˽ÓÐ,2,Ö»¶Á,3,Ö»¿Éд,4,¿É¶Á¿Éд
* @return ÊÇ·ñ±£´æ³É¹¦
*/
public static boolean saveUser(Context context,
String name, String password,int mode) {
OutputStream out=null;
try {
switch (mode) {
case 1:
out =context.openFileOutput("private.txt",context.MODE_PRIVATE);
break;
case 2:
out =context.openFileOutput("readable.txt",
context.MODE_WORLD_READABLE);
break;
case 3:
out =context.openFileOutput("writable.txt",
context.MODE_WORLD_WRITEABLE);
break;
case 4:
out =context.openFileOutput("public.txt",
context.MODE_WORLD_READABLE+context.MODE_WORLD_WRITEABLE);
break;
}
String data = name +"#"+password;
out.write(data.getBytes());
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static Map<String, String> getUser(Context
context) {
File file = new File(context.getFilesDir(), "user.txt");
/*
* ÅжÏÎļþÊÇ·ñ´æÔÚ
*/
if(!file.exists()){
return null;
}
/*
* »ñÈ¡Îļþ
*/
Map<String,String> map= new HashMap<String,
String>();
BufferedReader br=null;
try {
br = new BufferedReader(new InputStreamReader(new
FileInputStream(file)));
String data = br.readLine();
String[] split = data.split("#");
map.put("username", split[0]);
map.put("password", split[1]);
//·µ»Ø½á¹û
br.close();
return map;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//
return null;
}
} |
6.¶ÁÈ¡¸÷ÖÖȨÏÞµÄÎļþ
<LinearLayout 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="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity" >
<Button
android:id="@+id/btn_readPrivate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¶Áȡ˽ÓÐÎļþprivate" />
<Button
android:id="@+id/btn_writePrivate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="дÈë˽ÓÐÎļþprivate" />
<Button
android:id="@+id/btn_readReadable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¶ÁÈ¡¿É¶ÁÎļþreadable" />
<Button
android:id="@+id/btn_writeReadable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="дÈë¿É¶ÁÎļþreadable" />
<Button
android:id="@+id/btn_readWritable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¶ÁÈ¡¿ÉдÎļþWritable" />
<Button
android:id="@+id/btn_writeWritable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="дÈë¿ÉдÎļþWritable" />
<Button
android:id="@+id/btn_readPublic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¶ÁÈ¡¹«ÓÐÎļþPublic" />
<Button
android:id="@+id/btn_writePublic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="дÈ빫ÓÐÎļþPublic" />
</LinearLayout> |
package com.itheiam.readfile;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStream;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
/**
* ¶ÁÈ¡ºÍдÈë¸÷ÖÖȨÏÞϵÄÎļþ 07ºÅÏîÄ¿ µÄÎļþ
*
* @author Áõéª
*
* 2016-2-18ÏÂÎç6:33:00
*/
public class MainActivity extends Activity implements
OnClickListener {
/*
* ¶Áȡ˽ÓÐÎļþ°´¼ü
*/
private Button btn_readPrivate;
/*
* дÈë˽ÓÐÎļþ°´¼ü
*/
private Button btn_writePrivate;
/*
* ¶ÁÈ¡¿É¶ÁÎļþ
*/
private Button btn_readReadable;
/*
* дÈë¿É¶ÁÎļþ
*/
private Button btn_writeReadable;
/*
* ¶ÁÈ¡¿ÉдÎļþ
*/
private Button btn_readWritable;
/*
* дÈë¿ÉдÎļþ
*/
private Button btn_writeWritable;
/*
* ¶ÁÈ¡¹«ÓÐÎļþ
*/
private Button btn_readPublic;
/*
* дÈ빫ÓÐÎļþ
*/
private Button btn_writePublic;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//˽ÓÐÎļþ
btn_readPrivate = (Button) findViewById(R.id.btn_readPrivate);
btn_writePrivate = (Button) findViewById(R.id.btn_writePrivate);
//¿É¶ÁÎļþ
btn_readReadable = (Button) findViewById(R.id.btn_readReadable);
btn_writeReadable = (Button) findViewById(R.id.btn_writeReadable);
//¿ÉдÎļþ
btn_readWritable = (Button) findViewById(R.id.btn_readWritable);
btn_writeWritable = (Button) findViewById(R.id.btn_writeWritable);
//¹«ÓÐÎļþ
btn_readPublic = (Button) findViewById(R.id.btn_readPublic);
btn_writePublic = (Button) findViewById(R.id.btn_writePublic);
/*
* ÉèÖõ¥»÷ʼþ
*/
//˽ÓÐ
btn_readPrivate.setOnClickListener(this);
btn_writePrivate.setOnClickListener(this);
//¿É¶Á
btn_readReadable.setOnClickListener(this);
btn_writeReadable.setOnClickListener(this);
//¿Éд
btn_readWritable.setOnClickListener(this);
btn_writeWritable.setOnClickListener(this);
//¹«ÓÐ
btn_readPublic.setOnClickListener(this);
btn_writePublic.setOnClickListener(this);
}
/**
* µ¥»÷ʼþ¼àÌýÆ÷
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_readPrivate:
readPrivate();
break;
case R.id.btn_writePrivate:
writePrivate();
break;
case R.id.btn_readReadable:
readReadable();
break;
case R.id.btn_writeReadable:
writeReadable();
break;
case R.id.btn_readWritable:
readWritable();
break;
case R.id.btn_writeWritable:
writeWritable();
break;
case R.id.btn_readPublic:
readPublic();
break;
case R.id.btn_writePublic:
writePublic();
break;
}
}
/**
* дÈ빫ÓÐÎļþ
*/
private void writePublic() {
// »ñÈ¡¹«ÓÐÎļþ
File file = new File(
"/data/data/com.itheima.qqlogin/files/public.txt");
if (!file.exists()) {
Toast.makeText(this, "privateÎļþ²»´æÔÚ",
Toast.LENGTH_SHORT).show();
return;
}
// ¿ªÊ¼Ð´Èë
try {
OutputStream out = new FileOutputStream(file);
out.write("ÎÒдÈëÁË".getBytes());
out.close();
Toast.makeText(this, "дÈëpublic³É¹¦", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "дÈëpublicʧ°Ü", Toast.LENGTH_SHORT).show();
}
}
/**
* ¶ÁÈ¡¹«ÓÐÎļþ
*/
private void readPublic() {
// »ñÈ¡¹«ÓÐÎļþ
File file = new File(
"/data/data/com.itheima.qqlogin/files/public.txt");
if (!file.exists()) {
Toast.makeText(this, "publicÎļþ²»´æÔÚ",
Toast.LENGTH_SHORT).show();
return;
}
// ¿ªÊ¼¶ÁÈ¡
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String data = br.readLine();
Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
br.close();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "¶ÁÈ¡publicÎļþʧ°Ü",
Toast.LENGTH_SHORT).show();
}
}
/**
* дÈë¿ÉдÎļþ
*/
private void writeWritable() {
// »ñÈ¡¿ÉдÎļþ
File file = new File(
"/data/data/com.itheima.qqlogin/files/writable.txt");
if (!file.exists()) {
Toast.makeText(this, "writableÎļþ²»´æÔÚ",
Toast.LENGTH_SHORT).show();
return;
}
// ¿ªÊ¼Ð´Èë
try {
OutputStream out = new FileOutputStream(file);
out.write("ÎÒдÈëÁË".getBytes());
out.close();
Toast.makeText(this, "дÈëwritable³É¹¦",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "дÈëwritableʧ°Ü",
Toast.LENGTH_SHORT).show();
}
}
/**
* ¶ÁÈ¡¿ÉдÎļþ
*/
private void readWritable() {
// »ñÈ¡¿ÉдÎļþ
File file = new File(
"/data/data/com.itheima.qqlogin/files/writable.txt");
if (!file.exists()) {
Toast.makeText(this, "writableÎļþ²»´æÔÚ",
Toast.LENGTH_SHORT).show();
return;
}
// ¿ªÊ¼¶ÁÈ¡
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String data = br.readLine();
Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
br.close();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "¶ÁÈ¡writableÎļþʧ°Ü",
Toast.LENGTH_SHORT).show();
}
}
/**
* дÈë¿É¶ÁÎļþ
*/
private void writeReadable() {
// »ñÈ¡¿É¶ÁÎļþ
File file = new File(
"/data/data/com.itheima.qqlogin/files/readable.txt");
if (!file.exists()) {
Toast.makeText(this, "privateÎļþ²»´æÔÚ",
Toast.LENGTH_SHORT).show();
return;
}
// ¿ªÊ¼Ð´Èë
try {
OutputStream out = new FileOutputStream(file);
out.write("ÎÒдÈëÁË".getBytes());
out.close();
Toast.makeText(this, "дÈëreadable³É¹¦",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "дÈëreadableʧ°Ü",
Toast.LENGTH_SHORT).show();
}
}
/**
* ¶ÁÈ¡¿É¶ÁÎļþ
*/
private void readReadable() {
// »ñÈ¡¿É¶ÁÎļþ
File file = new File(
"/data/data/com.itheima.qqlogin/files/readable.txt");
if (!file.exists()) {
Toast.makeText(this, "privateÎļþ²»´æÔÚ",
Toast.LENGTH_SHORT).show();
return;
}
// ¿ªÊ¼¶ÁÈ¡
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String data = br.readLine();
Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
br.close();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "¶ÁÈ¡readableÎļþʧ°Ü",
Toast.LENGTH_SHORT).show();
}
}
/*
* ¶Áȡ˽ÓÐÎļþ
*/
private void readPrivate() {
// »ñȡ˽ÓÐÎļþ
File file = new File("/data/data/com.itheima.qqlogin/files/private.txt");
if (!file.exists()) {
Toast.makeText(this, "privateÎļþ²»´æÔÚ",
Toast.LENGTH_SHORT).show();
return;
}
// ¿ªÊ¼¶ÁÈ¡
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String data = br.readLine();
Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
br.close();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "¶ÁÈ¡privateÎļþʧ°Ü",
Toast.LENGTH_SHORT).show();
}
}
/*
* дÈë˽ÓÐÎļþ
*/
private void writePrivate() {
// »ñȡ˽ÓÐÎļþ
File file = new File("/data/data/com.itheima.qqlogin/files/private.txt");
if (!file.exists()) {
Toast.makeText(this, "privateÎļþ²»´æÔÚ",
Toast.LENGTH_SHORT).show();
return;
}
// ¿ªÊ¼Ð´Èë
try {
OutputStream out = new FileOutputStream(file);
out.write("ÎÒдÈëÁË".getBytes());
out.close();
Toast.makeText(this, "дÈëprivate³É¹¦",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "дÈëprivateʧ°Ü",
Toast.LENGTH_SHORT).show();
}
}
} |
7.»ñÈ¡´æ´¢¿Õ¼ä
<LinearLayout 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="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity" >
<Button
android:onClick="getRamSpace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="»ñÈ¡ÄÚ²¿´æ´¢¿Õ¼ä"/>
<Button
android:onClick="getSdSpace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="»ñÈ¡SD¿¨´æ´¢¿Õ¼ä"/>
<TextView
android:id="@+id/tv_ram"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_sd"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout> |
public class MainActivity extends Activity {
/*
* ÏÔʾÄÚ²¿¿Õ¼ä
*/
private TextView tv_sd;
/*
* ÏÔʾSD¿¨¿Õ¼ä
*/
private TextView tv_ram;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_ram = (TextView) findViewById(R.id.tv_ram);
tv_sd = (TextView) findViewById(R.id.tv_sd);
}
/**
* »ñÈ¡ÄÚ²¿´æ´¢¿Õ¼ä
* @param v
*/
public void getRamSpace(View v){
//×ܿռä
long totalSpace = Environment.getRootDirectory().getTotalSpace();
//¿ÉÓÿռä
long freeSpace = Environment.getRootDirectory().getFreeSpace();
//ÒѾʹÓõĿռä
long useSpace = totalSpace-freeSpace;
//×ܿռä´óС
String totalSize = Formatter.formatFileSize(this,
totalSpace);
//¿ÉÓÃ
String freeSize = Formatter.formatFileSize(this,
freeSpace);
//ÒѾʹÓÃ
String useSize = Formatter.formatFileSize(this,
useSpace);
String data="ÄÚ²¿´æ´¢×ܿռä:"+totalSize+",
ÒѾʹÓÃ:"+useSize+",¿ÉÓÿռä:"+freeSize;
tv_ram.setText(data);
}
/**
* »ñÈ¡SD´æ´¢¿Õ¼ä
* @param v
*/
public void getSdSpace(View v){
//×ܿռä
long totalSpace = Environment.getExternalStorageDirectory().getTotalSpace();
//¿ÉÓÿռä
long freeSpace = Environment.getExternalStorageDirectory().getFreeSpace();
//ÒѾʹÓõĿռä
long useSpace = totalSpace-freeSpace;
//×ܿռä´óС
String totalSize = Formatter.formatFileSize(this,
totalSpace);
//¿ÉÓÃ
String freeSize = Formatter.formatFileSize(this,
freeSpace);
//ÒѾʹÓÃ
String useSize = Formatter.formatFileSize(this,
useSpace);
String data="SD¿¨×ܿռä:"+totalSize+",
ÒѾʹÓÃ:"+useSize+",¿ÉÓÿռä:"+freeSize;
tv_sd.setText(data);
}
} |
8.ÉèÖÃÖÐÐÄ
<LinearLayout 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:background="#000" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="ÉùÒô"
android:textColor="#fff" />
<CheckBox
android:id="@+id/cb_sound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#55ffffff"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="ÏÔʾ"
android:textColor="#fff" />
<CheckBox
android:id="@+id/cb_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#55ffffff"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Ó¦ÓÃ"
android:textColor="#fff" />
<CheckBox
android:id="@+id/cb_app"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#55ffffff"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<SeekBar
android:id="@+id/sb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="30"
/>
<TextView
android:gravity="center_horizontal"
android:id="@+id/tv_sbProgress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textColor="#fff" />
</LinearLayout> |
package com.itheima.settingcenter;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
/**
* ʹÓÃSharedPreferences´æ´¢Ñ¡Ïî
* 1.³õʼ»¯
* 2.ÉèÖüàÌýÆ÷
* 3.±£´æ
* 4.È¡³ö
* @author Áõéª
*
* 2016-2-18ÏÂÎç8:44:14
*/
public class MainActivity extends Activity {
/*
* ÉùÒô¸´Ñ¡¿ò±£´æ
*/
private CheckBox cb_sound;
/*
* ÏÔʾ¸´Ñ¡¿ò
*/
private CheckBox cb_display;
/*
* app¸´Ñ¡¿ò±£´æ
*/
private CheckBox cb_app;
/*
* ½ø¶È
*/
private SeekBar sb;
/*
* ÉùÒô¸´Ñ¡¿ò±£´æ
*/
private SharedPreferences soundPreferences;
/*
* ÏÔʾ¸´Ñ¡¿ò
*/
private SharedPreferences displayPreferences;
/*
* app¸´Ñ¡¿ò±£´æ
*/
private SharedPreferences appPreferences;
/*
* seekbarµÄ½ø¶È±£´æ
*/
private SharedPreferences spPreferences;
/*
* ÏÔʾseekbar½ø¶È
*/
private TextView tv_sbProgress;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb_sound = (CheckBox) findViewById(R.id.cb_sound);
cb_display = (CheckBox) findViewById(R.id.cb_display);
cb_app = (CheckBox) findViewById(R.id.cb_app);
sb = (SeekBar) findViewById(R.id.sb);
tv_sbProgress = (TextView) findViewById(R.id.tv_sbProgress);
soundPreferences = getSharedPreferences("sound",
MODE_PRIVATE);
boolean soundChecked = soundPreferences.getBoolean("isChecked",
false);
cb_sound.setChecked(soundChecked);
displayPreferences = getSharedPreferences("display",
MODE_PRIVATE);
boolean displayChecked = displayPreferences.getBoolean("isChecked",
false);
cb_display.setChecked(displayChecked);
appPreferences = getSharedPreferences("app",
MODE_PRIVATE);
boolean appChecked = appPreferences.getBoolean("isChecked",
false);
cb_app.setChecked(appChecked);
spPreferences= getSharedPreferences("seekBar",
MODE_PRIVATE);
int progress = spPreferences.getInt("progress",
0);
sb.setProgress(progress);
/*
* ÉèÖüàÌýÆ÷
*/
cb_sound.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Editor editor = soundPreferences.edit();
editor.putBoolean("isChecked", isChecked);
editor.commit();
}
} );
/*
* ÉèÖüàÌýÆ÷
*/
cb_display.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Editor editor = displayPreferences.edit();
editor.putBoolean("isChecked", isChecked);
editor.commit();
}
} );
/*
* ÉèÖüàÌýÆ÷
*/
cb_app.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Editor editor = appPreferences.edit();
editor.putBoolean("isChecked", isChecked);
editor.commit();
}
} );
/*
* seekbar¼àÌýÆ÷
*/
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
int progress2 = seekBar.getProgress();
Editor editor = spPreferences.edit();
editor.putInt("progress", progress2);
editor.commit();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
tv_sbProgress.setText(seekBar.getProgress()+"");
}
@Override
public void onProgressChanged(SeekBar seekBar,
int progress,
boolean fromUser) {
tv_sbProgress.setText(progress+"");
}
});
}
} |
|