Android---MVP设计模式高级(三)

原文地址:http://blog.csdn.net/u013210620/article/details/50116753

点击打开链接,下载demo即可。。。


Android---MVP设计模式初级(一)

Android---MVP设计模式中级(二)


页面的布局样式如下:



先看数据库部分

DataBaseHelper这个类主要做的事情是:创建数据库、数据库表

[java]  view plain  copy
  1. package com.dandy.sqlite;  
  2.   
  3. import android.content.Context;  
  4. import android.database.sqlite.SQLiteDatabase;  
  5. import android.database.sqlite.SQLiteDatabase.CursorFactory;  
  6. import android.database.sqlite.SQLiteOpenHelper;  
  7. import android.text.TextUtils;  
  8.   
  9. public class DataBaseHelper extends SQLiteOpenHelper {  
  10.   
  11.     /** 
  12.      * 数据库名字 
  13.      */  
  14.     private static final String DATABASE_NAME = "dandy.db";  
  15.   
  16.     /** 
  17.      * 学生信息表名 
  18.      */  
  19.     protected static final String TABLE_NAME_STUDENT = "students";  
  20.   
  21.     /** 
  22.      * 老师信息表 
  23.      */  
  24.     protected static final String TABLE_NAME_TEACHER = "teachers";  
  25.   
  26.     /** 
  27.      * 在构造器里面,创建数据库的名字 
  28.      */  
  29.     public DataBaseHelper(Context context, int version) {  
  30.         this(context, DATABASE_NAME, null, version);  
  31.     }  
  32.   
  33.     public DataBaseHelper(Context context, String name, CursorFactory factory,  
  34.             int version) {  
  35.         super(context, DATABASE_NAME, factory, version);  
  36.     }  
  37.   
  38.     /** 
  39.      * 创建数据库 
  40.      */  
  41.     @Override  
  42.     public void onCreate(SQLiteDatabase db) {  
  43.         createTables(db);  
  44.     }  
  45.   
  46.     /** 
  47.      * 数据库升级回调方法 
  48.      */  
  49.     @Override  
  50.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  51.         if (oldVersion < newVersion) {  
  52.   
  53.             upgradeTables_method1(db, oldVersion);  
  54.         }  
  55.     }  
  56.   
  57.     /** 
  58.      * 表创建 
  59.      */  
  60.     private void createTables(SQLiteDatabase db) {  
  61.         createStudentTable(db);  
  62.     }  
  63.   
  64.     /** 
  65.      * 学生信息表的创建 public static final String CTEATETABLE = 
  66.      * "create table if not exists "; public static final String ID_AUTO = 
  67.      * " integer primary key autoincrement,"; public static final String TEXT = 
  68.      * " text "; public static final String TEXT_NOT_NULL = "text not null"; 
  69.      * public static final String LEFT_PARENTHESES = "("; public static final 
  70.      * String RIGHT_PARENTHESES = ")"; public static final String COMMA = ","; 
  71.      * public static final String ALTER = "alter table "; public static final 
  72.      * String RENAME = " rename to "; public static final String INSERT = 
  73.      * "insert into "; public static final String DROPTABLE = 
  74.      * "drop table if exists "; public static final String SELECT = " select "; 
  75.      * public static final String ADD = " add "; public static final String FROM 
  76.      * = " from "; public static final String SPACE = " "; public static final 
  77.      * String DEFAULT = " default "; 
  78.      *  
  79.      * sql拼接的语句如下: create table if not exists students(id integer primary key 
  80.      * autoincrement,name text,age text,sex default 0) 
  81.      */  
  82.     private void createStudentTable(SQLiteDatabase db) {  
  83.   
  84.         StringBuffer createStudentTableSql = new StringBuffer();  
  85.         createStudentTableSql.append(ConstantString.CTEATETABLE)  
  86.                 .append(TABLE_NAME_STUDENT)  
  87.                 .append(ConstantString.LEFT_PARENTHESES).append(BaseColum.ID)  
  88.                 .append(ConstantString.ID_AUTO).append(BaseColum.NAME)  
  89.                 .append(ConstantString.TEXT).append(ConstantString.COMMA)  
  90.                 .append(BaseColum.AGE).append(ConstantString.TEXT)  
  91.                 .append(ConstantString.COMMA).append(BaseColum.SEX)  
  92.                 .append(ConstantString.DEFAULT).append("0")  
  93.                 .append(ConstantString.RIGHT_PARENTHESES);  
  94.   
  95.         db.execSQL(createStudentTableSql.toString());  
  96.     }  
  97.   
  98.     /** 
  99.      * 老师信息表的创建 
  100.      */  
  101.     private void createTeacherTable(SQLiteDatabase db) {  
  102.         StringBuffer createTeacherTableSql = new StringBuffer();  
  103.         createTeacherTableSql.append(ConstantString.CTEATETABLE)  
  104.                 .append(TABLE_NAME_TEACHER)  
  105.                 .append(ConstantString.LEFT_PARENTHESES).append(BaseColum.ID)  
  106.                 .append(ConstantString.ID_AUTO).append(BaseColum.NAME)  
  107.                 .append(ConstantString.TEXT).append(ConstantString.COMMA)  
  108.                 .append(BaseColum.AGE).append(ConstantString.TEXT)  
  109.                 .append(ConstantString.RIGHT_PARENTHESES);  
  110.         db.execSQL(createTeacherTableSql.toString());  
  111.     }  
  112.   
  113.     /** 
  114.      * 数据库升级,确定相邻的2个版本之间的差异,数据库可以依次迭代,如:V1-->V2,V2-->V3 
  115.      *  
  116.      * 优点:每次更新数据库的时候只需要在onUpgrade方法的末尾加一段从上个版本升级到新版本的代码,易于理解和维护 
  117.      *  
  118.      * 缺点:当版本变多之后,多次迭代升级可能需要花费不少时间,增加用户等待 
  119.      */  
  120.     private void upgradeTables_method1(SQLiteDatabase db, int oldVersion) {  
  121.         int upgradeVersion = oldVersion;  
  122.   
  123.         // V1-->V2  
  124.         if (upgradeVersion == 1) {  
  125.             upgradeTables1(db);  
  126.             upgradeVersion = 2;  
  127.         }  
  128.   
  129.         // V2-->V3  
  130.         if (upgradeVersion == 2) {  
  131.             upgradeTables2(db);  
  132.         }  
  133.     }  
  134.   
  135.     /** 
  136.      * 数据库第一次升级,实现增加一张新表(如:teachers) 
  137.      */  
  138.     private void upgradeTables1(SQLiteDatabase db) {  
  139.         createTeacherTable(db);  
  140.     }  
  141.   
  142.     /** 
  143.      * 数据库第二次升级,新增表的列数(students表中增加address列) 
  144.      */  
  145.     private void upgradeTables2(SQLiteDatabase db) {  
  146.         try {  
  147.             db.beginTransaction();  
  148.             addTableColums(db, TABLE_NAME_STUDENT, BaseColum.ADD, "text",  
  149.                     "China");  
  150.             db.setTransactionSuccessful();  
  151.         } catch (Exception e) {  
  152.             e.printStackTrace();  
  153.         } finally {  
  154.             db.endTransaction();  
  155.         }  
  156.     }  
  157.   
  158.     /** 
  159.      * 增加数据库表中字段 
  160.      *  
  161.      * @param db 
  162.      * @param table 
  163.      *            :表名 
  164.      * @param coloum 
  165.      *            :字段名 
  166.      * @param type 
  167.      *            :字段属性 
  168.      * @param def 
  169.      *            :字段默认值 
  170.      */  
  171.     private void addTableColums(SQLiteDatabase db, String table, String colum,  
  172.             String type, String def) {  
  173.         try {  
  174.             StringBuffer addSql = new StringBuffer();  
  175.             addSql.append(ConstantString.ALTER)  
  176.                     .append(table)  
  177.                     .append(ConstantString.ADD)  
  178.                     .append(colum)  
  179.                     .append(type.startsWith(ConstantString.SPACE) ? type  
  180.                             : ConstantString.SPACE + type);  
  181.             if (!TextUtils.isEmpty(def)) {  
  182.                 if (def.contains("default")) {  
  183.                     addSql.append(def.startsWith(ConstantString.SPACE) ? def  
  184.                             : ConstantString.SPACE + def);  
  185.                 } else {  
  186.                     addSql.append(ConstantString.DEFAULT).append(def);  
  187.                 }  
  188.             }  
  189.             db.execSQL(addSql.toString());  
  190.         } catch (Exception e) {  
  191.             e.printStackTrace();  
  192.         }  
  193.     }  
  194.   
  195.     /** 
  196.      * 学生信息表字段名 
  197.      */  
  198.     static class BaseColum {  
  199.         public static final String ID = "id";  
  200.         public static final String NAME = "name";  
  201.         public static final String AGE = "age";  
  202.         public static final String SEX = "sex";  
  203.         public static final String ADD = "address";  
  204.     }  
  205.   
  206.     /** 
  207.      * 字符串常量 
  208.      */  
  209.     static class ConstantString {  
  210.         public static final String CTEATETABLE = "create table if not exists ";  
  211.         public static final String ID_AUTO = " integer primary key autoincrement,";  
  212.         public static final String TEXT = " text ";  
  213.         public static final String TEXT_NOT_NULL = "text not null";  
  214.         public static final String LEFT_PARENTHESES = "(";  
  215.         public static final String RIGHT_PARENTHESES = ")";  
  216.         public static final String COMMA = ",";  
  217.         public static final String ALTER = "alter table ";  
  218.         public static final String RENAME = " rename to ";  
  219.         public static final String INSERT = "insert into ";  
  220.         public static final String DROPTABLE = "drop table if exists ";  
  221.         public static final String SELECT = " select ";  
  222.         public static final String ADD = " add ";  
  223.         public static final String FROM = " from ";  
  224.         public static final String SPACE = " ";  
  225.         public static final String DEFAULT = " default ";  
  226.     }  
  227.   
  228. }  

AbstractSQLManager 这个类的方法是提供了数据库关闭,打开的操作

[java]  view plain  copy
  1. package com.dandy.sqlite;  
  2.   
  3. import android.content.Context;  
  4. import android.database.sqlite.SQLiteDatabase;  
  5.   
  6. import com.MvpApplication;  
  7. import com.dandy.util.AppUtil;  
  8.   
  9. /** 
  10.  * 这个类作用: 提供数据库的关闭,打开操作 
  11.  */  
  12. public class AbstractSQLManager {  
  13.   
  14.     private DataBaseHelper mDataBaseHelper;  
  15.   
  16.     private SQLiteDatabase mSQLitedb;  
  17.   
  18.     public AbstractSQLManager() {  
  19.         openDataBase(MvpApplication.getApplication());  
  20.     }  
  21.   
  22.     /** 
  23.      * 方法作用: 
  24.      * 创建数据库名字(有的话,就不会创建了)、并且获取getWritableDatabase 
  25.      */  
  26.     private void openDataBase(Context context) {  
  27.         if (mDataBaseHelper == null) {  
  28.             mDataBaseHelper = new DataBaseHelper(context,  
  29.                     AppUtil.getVersionCode(context));  
  30.         }  
  31.         if (mSQLitedb == null) {  
  32.             mSQLitedb = mDataBaseHelper.getWritableDatabase();  
  33.         }  
  34.     }  
  35.   
  36.     /** 
  37.      * @return 
  38.      * 创建数据库名字(有的话,就不会创建了)、并且获取getWritableDatabase 
  39.      */  
  40.     protected final SQLiteDatabase sqliteDB() {  
  41.         if (mSQLitedb == null) {  
  42.             openDataBase(MvpApplication.getApplication());  
  43.         }  
  44.         return mSQLitedb;  
  45.     }  
  46.   
  47.     /** 
  48.      * 数据库关闭 
  49.      */  
  50.     public void close() {  
  51.         try {  
  52.             if (mSQLitedb != null && mSQLitedb.isOpen()) {  
  53.                 mSQLitedb.close();  
  54.                 mSQLitedb = null;  
  55.             }  
  56.             if (mDataBaseHelper != null) {  
  57.                 mDataBaseHelper.close();  
  58.                 mDataBaseHelper = null;  
  59.             }  
  60.         } catch (Exception e) {  
  61.             e.printStackTrace();  
  62.         }  
  63.     }  
  64. }  

上个类中需要获取全局变量,我是自定义了一个application(如果有好的方法,给我留言,多谢)

MvpApplication

[java]  view plain  copy
  1. package com;  
  2.   
  3. import android.app.Application;  
  4.   
  5. public class MvpApplication extends Application{  
  6.       
  7.     private static MvpApplication mApplication;  
  8.       
  9.     @Override  
  10.     public void onCreate() {  
  11.         super.onCreate();  
  12.         MvpApplication.mApplication = this;  
  13.     }  
  14.   
  15.       
  16.     public static Application getApplication(){  
  17.         return mApplication;  
  18.     }  
  19. }     
还需要一个获取版本号的工具类

AppUtil

[java]  view plain  copy
  1. package com.dandy.util;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.nio.channels.FileChannel;  
  8.   
  9. import com.MvpApplication;  
  10.   
  11. import android.content.Context;  
  12. import android.content.pm.PackageInfo;  
  13. import android.content.pm.PackageManager.NameNotFoundException;  
  14. import android.os.Environment;  
  15.   
  16. public class AppUtil {  
  17.       
  18.     private static final String TAG = AppUtil.class.getSimpleName();  
  19.       
  20.     /** 
  21.      * 获取应用当前版本号 
  22.      * @param mContext  
  23.      */  
  24.     public static int getVersionCode(Context mContext) {  
  25.         int versionCode = 1;  
  26.         if(mContext == null) {  
  27.             return versionCode;  
  28.         }  
  29.         try {  
  30.             PackageInfo packageInfo = mContext.getPackageManager().getPackageInfo(  
  31.                     mContext.getPackageName(), 0);  
  32.             versionCode = packageInfo.versionCode;  
  33.         } catch (NameNotFoundException e) {  
  34.             e.printStackTrace();  
  35.         }  
  36.         return versionCode;  
  37.     }  
  38.       
  39.     /** 
  40.      * 拷贝数据库到sd卡 
  41.      *  
  42.      * @deprecated <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
  43.      */  
  44.     public static void copyDataBaseToSD(){  
  45.          if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {  
  46.              return ;  
  47.           }  
  48.          File dbFile = new File(MvpApplication.getApplication().getDatabasePath("dandy")+".db");  
  49.          File file  = new File(Environment.getExternalStorageDirectory(), "seeker.db");  
  50.            
  51.          FileChannel inChannel = null,outChannel = null;  
  52.            
  53.          try {  
  54.             file.createNewFile();  
  55.             inChannel = new FileInputStream(dbFile).getChannel();  
  56.             outChannel = new FileOutputStream(file).getChannel();  
  57.             inChannel.transferTo(0, inChannel.size(), outChannel);  
  58.         } catch (Exception e) {  
  59.             e.printStackTrace();  
  60.         }finally{  
  61.             try {  
  62.                 if (inChannel != null) {  
  63.                     inChannel.close();  
  64.                     inChannel = null;  
  65.                 }  
  66.                 if(outChannel != null){  
  67.                     outChannel.close();  
  68.                     outChannel = null;  
  69.                 }  
  70.             } catch (IOException e) {  
  71.                 e.printStackTrace();  
  72.             }  
  73.         }  
  74.     }  
  75.       
  76. }  


StudentSQLManager--这个类的作用是:提供一些业务方法

[java]  view plain  copy
  1. package com.dandy.sqlite;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import android.content.ContentValues;  
  6. import android.database.Cursor;  
  7. import android.text.TextUtils;  
  8. import com.dandy.bean.Student;  
  9. import com.dandy.sqlite.DataBaseHelper.BaseColum;  
  10.   
  11. public class StudentSQLManager extends AbstractSQLManager{  
  12.       
  13.     private static StudentSQLManager instance;  
  14.       
  15.     public static StudentSQLManager getInstance(){  
  16.         if(instance == null){  
  17.             synchronized (StudentSQLManager.class) {  
  18.                 if(instance == null){  
  19.                     instance = new StudentSQLManager();  
  20.                 }  
  21.             }  
  22.         }  
  23.         return instance;  
  24.     }  
  25.       
  26.     /** 
  27.      * 学生信息的插入 
  28.      * @param student  
  29.      */  
  30.     public void insertStudent(Student student){  
  31.         if(student == null){  
  32.             return;  
  33.         }  
  34.         ContentValues values = null;  
  35.         try {  
  36.             values = new ContentValues();  
  37.             values.put(BaseColum.NAME, student.getName());  
  38.             values.put(BaseColum.AGE, student.getAge());  
  39.             if(!TextUtils.isEmpty(student.getSex())){  
  40.                 values.put(BaseColum.SEX, student.getSex());  
  41.             }  
  42.             sqliteDB().insert(DataBaseHelper.TABLE_NAME_STUDENT, null, values);  
  43.         } catch (Exception e) {  
  44.             e.printStackTrace();  
  45.         }finally{  
  46.             if(values != null){  
  47.                 values.clear();  
  48.                 values = null;  
  49.             }  
  50.         }  
  51.     }  
  52.       
  53.     /** 
  54.      * 查找所有学生的信息  
  55.      */  
  56.     public List<Student> queryAllStudent(){  
  57.         List<Student> students = new ArrayList<Student>();  
  58.         Cursor cursor = null;  
  59.         try {  
  60.             cursor = sqliteDB().query(DataBaseHelper.TABLE_NAME_STUDENT, nullnullnullnullnullnull);  
  61.             if(cursor != null && cursor.getCount() >0 && cursor.moveToFirst()){  
  62.                 do {  
  63.                     Student student = new Student();  
  64.                     student.setId(cursor.getString(cursor.getColumnIndexOrThrow(BaseColum.ID)));  
  65.                     student.setName(cursor.getString(cursor.getColumnIndexOrThrow(BaseColum.NAME)));  
  66.                     student.setAge(cursor.getString(cursor.getColumnIndexOrThrow(BaseColum.AGE)));  
  67.                     student.setSex(cursor.getString(cursor.getColumnIndexOrThrow(BaseColum.SEX)));  
  68.                     students.add(student);  
  69.                 } while (cursor.moveToNext());  
  70.             }  
  71.         } catch (Exception e) {  
  72.             e.printStackTrace();  
  73.         }finally{  
  74.             if(cursor != null){  
  75.                 cursor.close();  
  76.                 cursor = null;  
  77.             }  
  78.         }  
  79.         return students;  
  80.     }  
  81.       
  82.     /** 
  83.      * 删除学生信息 
  84.      *   
  85.      * @param id 
  86.      */  
  87.     public int deleteStudent(String id){  
  88.         try {  
  89.             return sqliteDB().delete(DataBaseHelper.TABLE_NAME_STUDENT, BaseColum.ID+"=?"new String[]{id});  
  90.         } catch (Exception e) {  
  91.             e.printStackTrace();  
  92.         }  
  93.         return -1;  
  94.     }  
  95.       
  96.     /** 
  97.      * 更新学生信息 
  98.      *  
  99.      * @param student 
  100.      */  
  101.     public int updateStudent(Student student){  
  102.         ContentValues values = null;  
  103.         try {  
  104.             values = new ContentValues();  
  105.             values.put(BaseColum.NAME, student.getName());  
  106.             values.put(BaseColum.AGE, student.getAge());  
  107.             if(!TextUtils.isEmpty(student.getSex())){  
  108.                 values.put(BaseColum.SEX, student.getSex());  
  109.             }  
  110.             return sqliteDB().update(DataBaseHelper.TABLE_NAME_STUDENT, values, BaseColum.ID+"=?"new String[]{student.getId()});  
  111.         } catch (Exception e) {  
  112.             e.printStackTrace();  
  113.         }  
  114.         return -1;  
  115.     }  
  116. }  


接下来看M层

Student

[java]  view plain  copy
  1. package com.dandy.bean;  
  2.   
  3. public class Student {  
  4.       
  5.     private String id;  
  6.     private String name;  
  7.     private String age;  
  8.     private String sex;//male(0),female(1)  
  9.       
  10.     public String getId() {  
  11.         return id;  
  12.     }  
  13.     public void setId(String id) {  
  14.         this.id = id;  
  15.     }  
  16.       
  17.     public String getName() {  
  18.         return name;  
  19.     }  
  20.     public void setName(String name) {  
  21.         this.name = name;  
  22.     }  
  23.     public String getAge() {  
  24.         return age;  
  25.     }  
  26.     public void setAge(String age) {  
  27.         this.age = age;  
  28.     }  
  29.     public String getSex() {  
  30.         return sex;  
  31.     }  
  32.     public void setSex(String sex) {  
  33.         this.sex = sex;  
  34.     }  
  35.       
  36.     @Override  
  37.     public String toString() {  
  38.         return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]";  
  39.     }  
  40.       
  41. }  

M层对外提供的方法StudentModel

[java]  view plain  copy
  1. package com.dandy.bean;  
  2.   
  3. import java.util.List;  
  4. import com.dandy.bean.Student;  
  5. import com.dandy.sqlite.StudentSQLManager;  
  6.   
  7. public class StudentModel {  
  8.   
  9.     private StudentSQLManager manager;  
  10.       
  11.     public StudentModel() {  
  12.         this.manager = StudentSQLManager.getInstance();  
  13.     }  
  14.       
  15.     public void insertStudent(Student student) {  
  16.         manager.insertStudent(student);  
  17.     }  
  18.   
  19.     public List<Student> queryAllStudents() {  
  20.         return manager.queryAllStudent();  
  21.     }  
  22.   
  23.     public List<Student> queryStudentBySex(String sex) {  
  24.         return null;  
  25.     }  
  26.   
  27.     public int deleteStudent(String id) {  
  28.         return manager.deleteStudent(id);  
  29.     }  
  30.   
  31.     public int updateStudent(Student student) {  
  32.         return manager.updateStudent(student);  
  33.     }  
  34.   
  35. }  

接下来看V层

IStudentView

[java]  view plain  copy
  1. package com.dandy.view;  
  2.   
  3. import java.util.List;  
  4. import com.dandy.bean.Student;  
  5.   
  6. public interface IStudentView {  
  7.       
  8.     /** 
  9.      * 所有学生信息的获取展示 
  10.      * @params students  
  11.      */  
  12.     void showAllStudents(List<Student> students);  
  13.       
  14.     /** 
  15.      * 学生信息的删除 
  16.      * @params stident 
  17.      */  
  18.     void deleteStudent(Student student);  
  19.       
  20.     /** 
  21.      * 更新学生信息  
  22.      */  
  23.     void updateStudent(Student student);  
  24.       
  25.     /** 
  26.      * 获取学生姓名  
  27.      */  
  28.     String getName();  
  29.       
  30.     /** 
  31.      * 获取学生年龄  
  32.      */  
  33.     String getAge();  
  34.       
  35.     /** 
  36.      * 获取学生性别  
  37.      */  
  38.     String getSex();  
  39. }  

接下来看P层

StudentPresenter

[java]  view plain  copy
  1. package com.dandy.presenter;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.content.Context;  
  6. import android.text.TextUtils;  
  7. import android.widget.Toast;  
  8.   
  9. import com.dandy.bean.Student;  
  10. import com.dandy.bean.StudentModel;  
  11. import com.dandy.view.IStudentView;  
  12.   
  13. public class StudentPresenter {  
  14.   
  15.     private Context mContext;  
  16.     private IStudentView mStudentView;  
  17.     private StudentModel mStudentModel;  
  18.   
  19.     public StudentPresenter(Context mContext, IStudentView mStudentView) {  
  20.         this.mContext = mContext;  
  21.         this.mStudentView = mStudentView;  
  22.         this.mStudentModel = new StudentModel();  
  23.     }  
  24.   
  25.     /** 
  26.      * 保存学生的信息 
  27.      *  
  28.      * @param student 
  29.      */  
  30.     public void saveStudentInfo() {  
  31.         Student student = new Student();  
  32.         student.setName(mStudentView.getName());  
  33.         student.setAge(mStudentView.getAge());  
  34.         student.setSex(mStudentView.getSex());  
  35.         mStudentModel.insertStudent(student);  
  36.         Toast.makeText(mContext, student.toString(), Toast.LENGTH_SHORT).show();  
  37.     }  
  38.   
  39.     /** 
  40.      * 加载学生信息 
  41.      */  
  42.     public void loadAllStudent() {  
  43.         List<Student> students = mStudentModel.queryAllStudents();  
  44.         mStudentView.showAllStudents(students);  
  45.     }  
  46.   
  47.     /** 
  48.      * 根据性别加载学生信息 
  49.      *  
  50.      * @param sex 
  51.      */  
  52.     public void loadStudentsBySex(String sex) {  
  53.         if (TextUtils.isEmpty(sex)) {  
  54.             return;  
  55.         }  
  56.         List<Student> students = mStudentModel.queryStudentBySex(sex);  
  57.         mStudentView.showAllStudents(students);  
  58.     }  
  59.   
  60.     /** 
  61.      * 删除学生的信息 
  62.      *  
  63.      * @param student 
  64.      */  
  65.     public void deleteStudent(Student student) {  
  66.         if (student == null) {  
  67.             return;  
  68.         }  
  69.         String id = student.getId();  
  70.         if (TextUtils.isEmpty(id)) {  
  71.             return;  
  72.         }  
  73.         if (mStudentModel.deleteStudent(student.getId()) != -1) {  
  74.             mStudentView.deleteStudent(student);  
  75.         }  
  76.     }  
  77.   
  78.     /** 
  79.      * 更新学生信息 
  80.      *  
  81.      * @param student 
  82.      */  
  83.     public void updateStudent(Student student) {  
  84.         if (student == null) {  
  85.             return;  
  86.         }  
  87.         if (mStudentModel.updateStudent(student) != -1) {  
  88.             mStudentView.updateStudent(student);  
  89.         }  
  90.     }  
  91. }  


最后看主界面MainActivity

[java]  view plain  copy
  1. package com.example.mvpdemo;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import com.dandy.adapter.StudentAdapter;  
  6. import com.dandy.bean.Student;  
  7. import com.dandy.presenter.StudentPresenter;  
  8. import com.dandy.util.AppUtil;  
  9. import com.dandy.view.IStudentView;  
  10. import android.app.Activity;  
  11. import android.app.Dialog;  
  12. import android.os.Bundle;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.widget.AdapterView;  
  16. import android.widget.AdapterView.OnItemLongClickListener;  
  17. import android.widget.EditText;  
  18. import android.widget.ListView;  
  19.   
  20. public class MainActivity extends Activity implements IStudentView,OnClickListener,OnItemLongClickListener{  
  21.   
  22.     private StudentPresenter mStudentPresenter;  
  23.       
  24.     private EditText name,age,sex;  
  25.     private ListView studentList;  
  26.       
  27.     private List<Student> mStudents = new ArrayList<Student>();  
  28.     private StudentAdapter mStudentAdapter;  
  29.       
  30.     @Override  
  31.     protected void onCreate(Bundle savedInstanceState) {  
  32.         super.onCreate(savedInstanceState);  
  33.         setContentView(R.layout.activity_main);  
  34.           
  35.         mStudentPresenter = new StudentPresenter(thisthis);  
  36.           
  37.         findViewById(R.id.insert).setOnClickListener(this);  
  38.         findViewById(R.id.query).setOnClickListener(this);  
  39.         findViewById(R.id.copy).setOnClickListener(this);  
  40.           
  41.         name = (EditText) findViewById(R.id.name);  
  42.         age = (EditText) findViewById(R.id.age);  
  43.         sex = (EditText) findViewById(R.id.sex);  
  44.         studentList = (ListView) findViewById(R.id.student_list);  
  45.           
  46.         mStudentAdapter = new StudentAdapter(this, mStudents, R.layout.student_info_item_layout);  
  47.         studentList.setAdapter(mStudentAdapter);  
  48.           
  49.         studentList.setOnItemLongClickListener(this);  
  50.     }  
  51.   
  52.     /** 
  53.      * implements IStudentView 
  54.      */  
  55.     @Override  
  56.     public void showAllStudents(List<Student> students) {  
  57.         mStudents.clear();  
  58.         mStudents.addAll(students);  
  59.         mStudentAdapter.notifyDataSetChanged();  
  60.     }  
  61.   
  62.     @Override  
  63.     public void deleteStudent(Student student) {  
  64.         mStudentPresenter.loadAllStudent();  
  65.     }  
  66.   
  67.     @Override  
  68.     public void updateStudent(Student student) {  
  69.         mStudentPresenter.loadAllStudent();  
  70.     }  
  71.       
  72.     @Override  
  73.     public String getName() {  
  74.         return name.getText().toString();  
  75.     }  
  76.   
  77.     @Override  
  78.     public String getAge() {  
  79.         return age.getText().toString();  
  80.     }  
  81.   
  82.     @Override  
  83.     public String getSex() {  
  84.         return sex.getText().toString();  
  85.     }  
  86.       
  87.     @SuppressWarnings("deprecation")  
  88.     @Override  
  89.     public void onClick(View v) {  
  90.         switch (v.getId()) {  
  91.         case R.id.insert:  
  92.             mStudentPresenter.saveStudentInfo();  
  93.             break;  
  94.   
  95.         case R.id.query:  
  96.             mStudentPresenter.loadAllStudent();  
  97.             break;  
  98.         case R.id.copy:  
  99.             AppUtil.copyDataBaseToSD();  
  100.             break;  
  101.         }  
  102.     }  
  103.   
  104.     /** 
  105.      * 实现listView的item长按事件的回调方法 
  106.      */  
  107.     @Override  
  108.     public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) {  
  109.         final Student student = mStudents.get(position);  
  110.         final Dialog dialog = new Dialog(this, R.style.DialogStyle);  
  111.         dialog.setContentView(R.layout.student_edit_layout);  
  112.         dialog.show();  
  113.         dialog.findViewById(R.id.delete).setOnClickListener(new OnClickListener() {  
  114.               
  115.             @Override  
  116.             public void onClick(View v) {  
  117.                 mStudentPresenter.deleteStudent(student);  
  118.                 dialog.cancel();  
  119.             }  
  120.         });  
  121.         dialog.findViewById(R.id.update).setOnClickListener(new OnClickListener() {  
  122.               
  123.             @Override  
  124.             public void onClick(View v) {  
  125.                 mStudentPresenter.updateStudent(student);  
  126.                 dialog.cancel();  
  127.             }  
  128.         });  
  129.         dialog.findViewById(R.id.cancle).setOnClickListener(new OnClickListener() {  
  130.       
  131.             @Override  
  132.             public void onClick(View v) {  
  133.                 dialog.cancel();  
  134.             }  
  135.         });  
  136.         return true;  
  137.     }  
  138. }  

另外在看看通用的adapter的使用

ViewHolder

[java]  view plain  copy
  1. package com.dandy.adapter;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.drawable.Drawable;  
  6. import android.text.SpannableString;  
  7. import android.util.SparseArray;  
  8. import android.view.LayoutInflater;  
  9. import android.view.View;  
  10. import android.view.ViewGroup;  
  11. import android.widget.CompoundButton;  
  12. import android.widget.ImageView;  
  13. import android.widget.TextView;  
  14.   
  15. public class ViewHolder{  
  16.     private final SparseArray<View> mViews;  
  17.     private int mPosition;  
  18.     private View mConvertView;  
  19.     private int itemViewType;  
  20.   
  21.     private ViewHolder(Context context, ViewGroup parent, int layoutId,int position,int itemViewType) {  
  22.         this.mPosition = position;  
  23.         this.itemViewType = itemViewType;  
  24.         this.mViews = new SparseArray<View>();  
  25.         this.mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,false);  
  26.         // setTag  
  27.         this.mConvertView.setTag(this);  
  28.     }  
  29.   
  30.     /** 
  31.      * 拿到一个ViewHolder对象 
  32.      * @param context 
  33.      * @param convertView 
  34.      * @param parent 
  35.      * @param layoutId 
  36.      * @param position 
  37.      * @return 
  38.      */  
  39.     public static ViewHolder get(Context context, View convertView,ViewGroup parent, int layoutId, int position,int itemViewType) {  
  40.         if (convertView == null) {  
  41.             return new ViewHolder(context, parent, layoutId, position,itemViewType);  
  42.         }         
  43.         ViewHolder viewHolder = (ViewHolder) convertView.getTag();  
  44.         if(viewHolder != null && viewHolder.getItemViewType() == itemViewType){  
  45.             return viewHolder;  
  46.         }else{  
  47.             viewHolder = null;  
  48.         }  
  49.         return viewHolder;  
  50.     }  
  51.   
  52.     public int getItemViewType() {  
  53.         return itemViewType;  
  54.     }  
  55.       
  56.     public View getConvertView() {  
  57.         return this.mConvertView;  
  58.     }  
  59.   
  60.     /** 
  61.      * 通过控件的Id获取对于的控件,如果没有则加入views 
  62.      * @param viewId 
  63.      * @return 
  64.      */  
  65.     @SuppressWarnings("unchecked")  
  66.     public <T extends View> T getView(int viewId) {  
  67.         View view = mViews.get(viewId);  
  68.         if (view == null) {  
  69.             view = mConvertView.findViewById(viewId);  
  70.             mViews.put(viewId, view);  
  71.         }  
  72.         return (T) view;  
  73.     }  
  74.       
  75.     /** 
  76.      *通过控件的Id控制控件是否显示 
  77.      * @param viewId  
  78.      * @param visibility 
  79.      */  
  80.     public void setVisibility(int viewId,int visibility){  
  81.         View view = getView(viewId);  
  82.         view.setVisibility(visibility);  
  83.     }  
  84.       
  85.     /** 
  86.      *通过控件的Id控制控件是否可点击 
  87.      * @param viewId  
  88.      * @param visibility 
  89.      */  
  90.     public void setClickable(int viewId,boolean clickable){  
  91.         View view = getView(viewId);  
  92.         view.setClickable(clickable);  
  93.     }  
  94.       
  95.     /** 
  96.      * 为TextView设置字符串 
  97.      * @param viewId 
  98.      * @param charSequence 
  99.      * @param visible 
  100.      * @return 
  101.      */  
  102.     public void setText(int viewId, CharSequence charSequence) {  
  103.         TextView view = getView(viewId);  
  104.         view.setText(charSequence);  
  105.     }  
  106.   
  107.     public void setText(int viewId, SpannableString text) {  
  108.         TextView view = getView(viewId);  
  109.         view.setText(text);  
  110.     }  
  111.   
  112.   
  113.     public void setImageBitmap(int viewId, Bitmap bm){  
  114.         ImageView view = getView(viewId);  
  115.         if(view != null && bm != null){  
  116.             view.setImageBitmap(bm);  
  117.         }  
  118.     }  
  119.     public void setImageDrawable(int viewId, Drawable draw){  
  120.         ImageView view = getView(viewId);  
  121.         if(view != null && draw != null){  
  122.             view.setImageDrawable(draw);  
  123.         }  
  124.     }  
  125.       
  126.     /** 
  127.      *  
  128.      */  
  129.     public void setEnable(int viewId, boolean enable){  
  130.         View view = getView(viewId);  
  131.         view.setEnabled(enable);  
  132.     }  
  133.       
  134.     public void setChecked(int viewId, boolean checked){  
  135.         CompoundButton cb = getView(viewId);  
  136.         cb.setChecked(checked);  
  137.     }  
  138.       
  139.     public int getPosition() {  
  140.         return mPosition;  
  141.     }  
  142. }  


CommonAdapter

[java]  view plain  copy
  1. package com.dandy.adapter;  
  2.   
  3. import java.util.Arrays;  
  4. import java.util.List;  
  5.   
  6. import android.content.Context;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.BaseAdapter;  
  10.   
  11. /** 
  12.  * 抽象类 
  13.  * 通用的adapter适配器 
  14.  */  
  15. public abstract class CommonAdapter<T> extends BaseAdapter{  
  16.       
  17.     public Context mContext;  
  18.     private List<T> mDatas;  
  19.     private int [] itemLayoutIds;  
  20.     private int [] viewTypes;  
  21.     private static final int defaultViewType = 0x00;  
  22.       
  23.     /** 
  24.      *  
  25.      * @param context 环境 
  26.      * @param mDatas 数据源 
  27.      * @param itemLayoutId 对应布局格式的布局数量--本例只有1个 
  28.      */  
  29.     public CommonAdapter(Context context, List<T> mDatas, int itemLayoutId) {  
  30.         this.mContext = context;  
  31.         this.mDatas = mDatas;  
  32.         //布局格式数量--及其对应布局格式的布局数量本例都为1  
  33.         this.itemLayoutIds = new int[1];  
  34.         this.viewTypes = new int[1];  
  35.         //获取本例的布局格式、及其对应布局格式的布局数量  
  36.         this.itemLayoutIds[0] = itemLayoutId;  
  37.         this.viewTypes[0] = defaultViewType;  
  38.     }  
  39.     /** 
  40.      *  
  41.      * @param context 环境变量 
  42.      * @param mDatas 数据源 
  43.      * @param itemLayoutIds 对应布局格式的布局数量 
  44.      * @param viewTypes 布局格式数量 
  45.      */  
  46.     public CommonAdapter(Context context, List<T> mDatas,int [] itemLayoutIds,int [] viewTypes) {  
  47.         this.mContext = context;  
  48.         this.mDatas = mDatas;  
  49.         this.itemLayoutIds = itemLayoutIds;  
  50.         this.viewTypes = viewTypes;  
  51.     }  
  52.     @Override  
  53.     public int getCount() {  
  54.         return mDatas.size();  
  55.     }  
  56.   
  57.     @Override  
  58.     public T getItem(int position) {  
  59.         return mDatas.get(position);  
  60.     }  
  61.   
  62.     @Override  
  63.     public long getItemId(int position) {  
  64.         return position;  
  65.     }  
  66.     //返回你有多少个不同的布局  
  67.     @Override  
  68.     public int getViewTypeCount() {  
  69.         return viewTypes.length;  
  70.     }  
  71.       
  72.     /** 
  73.      * 多种布局格式需要重写此方法  
  74.      * 
  75.      * 因为 The item view type you are returning from getItemViewType() is >= getViewTypeCount(), 
  76.      * 所以type的最大值最好从0开始,然后++ 
  77.      */  
  78.     public int getItemViewType(int position,int [] viewTypes){  
  79.         return defaultViewType;  
  80.     };  
  81.     /** 
  82.      *  
  83.      */  
  84.     @Override  
  85.     public int getItemViewType(int position) {  
  86.         return getItemViewType(position,viewTypes);  
  87.     }  
  88.       
  89.     @Override  
  90.     public View getView(int position, View convertView, ViewGroup parent) {  
  91.           
  92.         int viewType = getItemViewType(position);  
  93.         int index = Arrays.binarySearch(viewTypes, viewType);  
  94.           
  95.         ViewHolder viewHolder = getViewHolder(position, convertView,parent,itemLayoutIds[index],viewType);  
  96.         convert(viewHolder, getItem(position),viewType, position);  
  97.         return viewHolder.getConvertView();  
  98.     }  
  99.   
  100.     public abstract void convert(ViewHolder viewHolder, T item,int itemViewType, int position);  
  101.   
  102.     private ViewHolder getViewHolder(int position, View convertView,ViewGroup parent,int layoutId,int itemViewType) {  
  103.         return ViewHolder.get(mContext, convertView, parent,layoutId,position,itemViewType);  
  104.     }  
  105. }  

StudentAdapter

[java]  view plain  copy
  1. package com.dandy.adapter;  
  2.   
  3. import java.util.List;  
  4. import android.content.Context;  
  5. import com.dandy.bean.Student;  
  6. import com.example.mvpdemo.R;  
  7.   
  8. public class StudentAdapter extends CommonAdapter<Student>{  
  9.   
  10.     public StudentAdapter(Context context, List<Student> mDatas,int itemLayoutId) {  
  11.         super(context, mDatas, itemLayoutId);  
  12.     }  
  13.   
  14.     @Override  
  15.     public void convert(ViewHolder viewHolder, Student item, int itemViewType,int position) {  
  16.         String sex = "";  
  17.         if(item.getSex().equals("0")){  
  18.             sex = "男";  
  19.         }else{  
  20.             sex = "女";  
  21.         }  
  22.         viewHolder.setText(R.id.student_info, item.getName()+"_"+item.getAge()+"_"+sex);  
  23.     }  
  24.   
  25. }  

activity_main.xml

[html]  view plain  copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <EditText  
  8.         android:id="@+id/name"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:hint="请输入姓名" />  
  12.   
  13.     <EditText  
  14.         android:id="@+id/age"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:hint="请输入年龄"   
  18.         android:inputType="number"/>  
  19.   
  20.     <EditText  
  21.         android:id="@+id/sex"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:hint="请输入性别:0(男)1(女)"   
  25.         android:inputType="number"  
  26.         android:digits="0,1"/>  
  27.   
  28.     <LinearLayout  
  29.         android:layout_width="match_parent"  
  30.         android:layout_height="wrap_content"  
  31.         android:gravity="center_horizontal"  
  32.         android:orientation="horizontal" >  
  33.   
  34.         <Button  
  35.             android:id="@+id/insert"  
  36.             android:layout_width="wrap_content"  
  37.             android:layout_height="wrap_content"  
  38.             android:text="插入" />  
  39.   
  40.         <Button  
  41.             android:id="@+id/query"  
  42.             android:layout_width="wrap_content"  
  43.             android:layout_height="wrap_content"  
  44.             android:layout_marginLeft="20dp"  
  45.             android:text="查找" />  
  46.         <Button  
  47.             android:id="@+id/copy"  
  48.             android:layout_width="wrap_content"  
  49.             android:layout_height="wrap_content"  
  50.             android:layout_marginLeft="20dp"  
  51.             android:text="拷贝" />  
  52.     </LinearLayout>  
  53.   
  54.     <ListView  
  55.         android:id="@+id/student_list"  
  56.         android:layout_width="match_parent"  
  57.         android:layout_height="wrap_content" />  
  58.   
  59. </LinearLayout>  

student_info_item_layout

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/student_info"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="40dp"  
  11.         android:gravity="center_vertical"  
  12.         android:paddingLeft="20dp"  
  13.         android:text="dandy:24:男" />  
  14.   
  15. </LinearLayout>  


student_edit_layout

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="210dp"  
  4.     android:layout_height="wrap_content"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/update"  
  9.         style="@style/StudentEditStyle"  
  10.         android:background="@drawable/selector_item_back_top"  
  11.         android:text="更新" />  
  12.   
  13.     <TextView  
  14.         android:id="@+id/delete"  
  15.         style="@style/StudentEditStyle"  
  16.         android:background="@drawable/selector_item_back_center"  
  17.         android:text="删除" />  
  18.   
  19.     <TextView  
  20.         android:id="@+id/cancle"  
  21.         style="@style/StudentEditStyle"  
  22.         android:background="@drawable/selector_item_back_bottom"  
  23.         android:text="取消" />  
  24.   
  25. </LinearLayout>  




style.xml 就是定义dialog的样式的

[html]  view plain  copy
  1. <style name="CenterStyle">  
  2.        <item name="android:gravity">center</item>  
  3.    </style>  
  4.      
  5.    <style name="SizeStyle" parent="CenterStyle">  
  6.        <item name="android:layout_width">match_parent</item>  
  7.        <item name="android:layout_height">50dp</item>  
  8.    </style>  
  9.      
  10.    <style name="StudentEditStyle" parent="SizeStyle">  
  11.        <item name="android:textSize">16sp</item>  
  12.        <item name="android:textColor">#111111</item>  
  13.    </style>  
  14.      
  15.    <style name="DialogStyle">  
  16.         <item name="android:windowFrame">@null</item>  <!-- 边框 -->    
  17.         <item name="android:windowIsFloating">true</item>  <!-- 是否浮现在activity之上 -->    
  18.     <item name="android:windowIsTranslucent">true</item>  <!-- 半透明 -->    
  19.         <item name="android:windowNoTitle">true</item>   <!-- 无标题 -->    
  20.     <item name="android:windowBackground">@android:color/transparent</item>   <!-- 背景透明 -->    
  21.         <item name="android:backgroundDimEnabled">true</item>   <!-- 模糊 -->    
  22.    </style>  


更新的

selector_item_back_top.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <item android:drawable="@drawable/layer_rect_top_corner_press" android:state_pressed="true"/>  
  5.     <item android:drawable="@drawable/layer_rect_top_corner_normal" android:state_pressed="false"/>  
  6.     <item android:drawable="@drawable/layer_rect_top_corner_normal"/>  
  7.   
  8. </selector>  

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.       
  4.     <item android:bottom="@dimen/devider">  
  5.         <shape android:shape="rectangle">  
  6.             <corners android:topLeftRadius="@dimen/corner" android:topRightRadius="@dimen/corner"   
  7.                      android:bottomLeftRadius="0dp" android:bottomRightRadius="0dp"/>  
  8.   
  9.             <solid android:color="@color/color_press" />  
  10.   
  11.             <stroke android:width="@dimen/devider" android:color="@color/color_devider" />  
  12.         </shape>  
  13.     </item>  
  14.   
  15. </layer-list>  

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.       
  4.     <item android:bottom="@dimen/devider">  
  5.         <shape android:shape="rectangle">  
  6.             <corners android:topLeftRadius="@dimen/corner" android:topRightRadius="@dimen/corner"   
  7.                      android:bottomLeftRadius="0dp" android:bottomRightRadius="0dp"/>  
  8.   
  9.             <solid android:color="@color/color_white" />  
  10.   
  11.             <stroke android:width="@dimen/devider" android:color="@color/color_devider" />  
  12.         </shape>  
  13.     </item>  
  14.   
  15. </layer-list>  

删除

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <item android:state_pressed="true">  
  5.         <shape android:shape="rectangle">  
  6.             <corners android:radius="0dp" />  
  7.             <solid android:color="@color/color_press" />  
  8.             <stroke android:width="@dimen/devider" android:color="@color/color_devider" />  
  9.         </shape>  
  10.     </item>  
  11.       
  12.     <item android:state_pressed="false">  
  13.         <shape android:shape="rectangle">  
  14.             <corners android:radius="0dp" />  
  15.             <solid android:color="@color/color_white" />  
  16.             <stroke android:width="@dimen/devider" android:color="@color/color_devider" />  
  17.         </shape>  
  18.     </item>  
  19.       
  20.     <item>  
  21.         <shape android:shape="rectangle">  
  22.             <corners android:radius="0dp" />  
  23.             <solid android:color="@color/color_white" />  
  24.             <stroke android:width="@dimen/devider" android:color="@color/color_devider" />  
  25.         </shape></item>  
  26.   
  27. </selector>  

取消

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <item android:top="@dimen/devider">  
  4.         <shape android:shape="rectangle">  
  5.             <corners android:bottomLeftRadius="@dimen/corner" android:bottomRightRadius="@dimen/corner"   
  6.                      android:topLeftRadius="0dp" android:topRightRadius="0dp"/>  
  7.   
  8.             <solid android:color="@color/color_press" />  
  9.   
  10.             <stroke android:width="@dimen/devider" android:color="@color/color_devider" />  
  11.         </shape>  
  12.     </item>  
  13.   
  14. </layer-list>  

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <item android:top="@dimen/devider">  
  4.         <shape android:shape="rectangle">  
  5.             <corners android:bottomLeftRadius="@dimen/corner" android:bottomRightRadius="@dimen/corner"   
  6.                      android:topLeftRadius="0dp" android:topRightRadius="0dp"/>  
  7.   
  8.             <solid android:color="@color/color_white" />  
  9.   
  10.             <stroke android:width="@dimen/devider" android:color="@color/color_devider" />  
  11.         </shape>  
  12.     </item>  
  13.   
  14. </layer-list>  


尺寸
[html]  view plain  copy
  1. <resources>  
  2.   
  3.     <!-- Default screen margins, per the Android Design guidelines. -->  
  4.     <dimen name="activity_horizontal_margin">16dp</dimen>  
  5.     <dimen name="activity_vertical_margin">16dp</dimen>  
  6.   
  7.     <dimen name="corner">10dp</dimen>  
  8.     <dimen name="devider">0.01dp</dimen>  
  9.     <dimen name="textSize1">12sp</dimen>  
  10.       
  11. </resources>  
颜色
[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.       
  4.     <color name="color_devider">#cccccc</color>  
  5.     <color name="color_white">#ffffff</color>  
  6.     <color name="color_press">#e5e5e5</color>  
  7. </resources>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值