編輯:關於Android編程
完成效果:選擇選項後退出,界面記住選擇選項。
1.利用SharePreferences方法記住選擇的選項
主布局:
布局效果:
代碼:
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); } }2.將選擇的選項寫入到Sd卡中 代碼:
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(); } } } } }效果展示:
CoordinatorLayout簡介可以看到該控件是Support Design包中的一個非常重要的控件,Google官方將CoordinatorLayout稱為是一個
Android開發之MVP模式的學習:提問:明白如何選擇開發框架,和為什麼要學MVP模式觀察:比較MVC模式和MVP模式,理解MVP模式的概念使用:通過一個例子,學習如何
目標:雙向拖動的自定義View國際慣例先預覽後實現實現步驟自定義屬性的抽取 view尺寸的計算 相關內容的繪制(文字,原點,背景進度條,當前進度條等等) 處理滑動事件大體
項目下載地址:https://github.com/Aiushtha/android-PictureSelector最早使用android調用系統拍照然後遇到很多空指針等