首页 > 代码库 > Day02

Day02

1、数据库的创建以及增删改查

创建---继承SqliteOpenHelper类

创建Dao接口

long insert(…);//返回记录插入出的索引,若返回-1表示插入记录失败

int delete(…);//返回删除了几条记录,若返回0表示删除失败

int update(…);//返回修改了几条记录,若返回0表示修改失败

boolean query(…);//Cursor一定要记得关闭

List<T> getAll();

常用语句

建表

db.execSQL("create table student (_id integer primary key autoincrement,name varchar(20), sex varchar(6))");

db.execSQL("insert into student (name,sex) values (?,?)", new Object[]{name,sex});

db.execSQL("delete from student where name=?",new Object[]{name});

db.execSQL("update student set sex =? where name=?",new Object[]{newsex,name});

Cursor cursor = db.rawQuery("select sex from student where name=?", new String[]{name});

 

2、数据库的事务

    ----可以保证在同一数据库里的操作要么同时成功,要么同时失败

    示例代码:

  1. BankDBOpenHelper helper = new BankDBOpenHelper(this);
  2. SQLiteDatabase db = helper.getWritableDatabase();
  3. db.beginTransaction(); // 开启事务
  4. try {
  5.    // 模拟转账的操作---需要要么同时成功要么同时失败的操作
  6.    db.execSQL("update account set money=money-100 where name=‘zhangsan‘");
  7.    db.execSQL("update account set money=money+100 where name=‘lisi‘");
  8.    db.setTransactionSuccessful();// 设置事务执行成功
  9. } finally {
  10.    db.endTransaction();
  11. }
  12. db.close();

 

3、listview---baseAdater、arrayAdapter、simpleAdapter

    BaseAdapter里面的getCount()和getView()必须重写,且最好有一个接受Context和数据集合的构造参数

    ArrayAdapter接收的参数是数组和item的布局文件

    SimpleAdapter接受的是一个List集合,这个List集合里的每一个元素都是Map集合,而每个Map集合的元素都与资源数组中的id值相对应

4、五种对话框    

1.确定取消对话框---AlertDialog

  1. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  2. AlertDialog dialog = builder.setTitle("警告:")
  3.         .setMessage("您将要进入凶险的新世界")
  4.         .setPositiveButton("继续冒险"
  5.                 , new DialogInterface.OnClickListener() {
  6.                     @Override
  7.                     public void onClick(DialogInterface dialog, int which) {
  8.                         Toast.makeText(MainActivity.this, "继续冒险", Toast.LENGTH_SHORT).show();
  9.                     }
  10.                 })
  11.         .setNegativeButton("不再冒险"
  12.                 , new DialogInterface.OnClickListener() {
  13.                     @Override
  14.                     public void onClick(DialogInterface dialog, int which) {
  15.                         Toast.makeText(MainActivity.this, "不再冒险", Toast.LENGTH_SHORT).show();
  16.                     }
  17.                 })
  18.         .create();
  19. dialog.show();

2.单选对话框---AlertDialog

  1. AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
  2. final String[] fruits = {"烧烧果实", "橡胶果实", "透明果实"};
  3. AlertDialog dialog1 = builder1
  4.         .setTitle("选择您要吃的果实")
  5.         .setSingleChoiceItems(fruits, -1
  6.                 , new DialogInterface.OnClickListener() {
  7.                     @Override
  8.                     public void onClick(DialogInterface dialog, int which) {
  9.                         Toast.makeText(MainActivity.this, "您选择了" + fruits[which], Toast.LENGTH_SHORT).show();
  10.                         dialog.dismiss();
  11.                     }
  12.                 })
  13.         .setNegativeButton("取消选择", new DialogInterface.OnClickListener() {
  14.             @Override
  15.             public void onClick(DialogInterface dialog, int which) {
  16.                 Toast.makeText(MainActivity.this, "不,我哪个都不吃,我要游泳", Toast.LENGTH_SHORT).show();
  17.             }
  18.         })
  19.         .create();
  20. dialog1.show();

3、多选对话框---AlertDialog

  1. final String[] miji = {"九阳神功", "蛤蟆功", "九阴真经", "葵花宝典", "七十二变", "天照"};
  2. boolean[] checkedChoice = {true, false, true, false, false, true};
  3. AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
  4. builder2.setTitle("请选择您想学的武功")
  5.         .setMultiChoiceItems(miji, checkedChoice
  6.                 , new DialogInterface.OnMultiChoiceClickListener() {
  7.                     @Override
  8.                     public void onClick(DialogInterface dialog, int which, boolean isChecked) {
  9.                         Toast.makeText(MainActivity.this, miji[which] + "----" + isChecked, Toast.LENGTH_SHORT).show();
  10.                     }
  11.                 })
  12.         .setNegativeButton("取消", null)
  13.         .create()
  14.         .show();

4、圆形进度对话框---ProgressDialog

  1. final ProgressDialog pd = new ProgressDialog(this);
  2. pd.setTitle("提醒");
  3. pd.setMessage("正在加载中。。。");
  4. pd.show();
  5. new Thread() {
  6.     @Override
  7.     public void run() {
  8.         try {
  9.             Thread.sleep(5000);
  10.         } catch (InterruptedException e) {
  11.             e.printStackTrace();
  12.         }
  13.         pd.dismiss();
  14.     }
  15. }.start();

5、条形进度对话框---ProgressDialog

  1. final ProgressDialog pd1 = new ProgressDialog(this);
  2. pd1.setTitle("警告");
  3. pd1.setMessage("正在加载数据");
  4. pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  5. pd1.setMax(100);
  6. pd1.show();
  7. new Thread() {
  8.     @Override
  9.     public void run() {
  10.         for (int i = 1; i < 100; i++) {
  11.             try {
  12.                 Thread.sleep(50);
  13.             } catch (InterruptedException e) {
  14.                 e.printStackTrace();
  15.             }
  16.             pd1.setProgress(i);
  17.         }
  18.         pd1.dismiss();
  19.     }
  20. }.start();

 

5、样式、主题

    样式是用来改变指定控件或layout的大小和外观的一组属性的集合;

    Style------样式是针对空间的属性的

    Theme-----主题是针对activity的Layout布局的,主题是样式的一种

    作用:节省代码,可以批量改变控件的大小和外观

Style示例,提取样式refactor—>extraàstyle 快捷键alt+y

  1. <style name="text_content_style">
  2.     <item name="android:layout_width">match_parent</item>
  3.     <item name="android:layout_height">wrap_content</item>
  4.     <item name="android:textColor">#0000ff</item>
  5.     <item name="android:textSize">20sp</item>
  6. </style>

theme示例

  1. <style name="my_app_theme">
  2.           <item name="android:windowNoTitle">true</item>
  3.           <item name="android:background">#440000ff</item>
  4.     </style>

 

6、样式的继承

1、parent属性

  1. <style name="AppTheme" parent="AppBaseTheme">
  2.      <item name="android:windowNoTitle">true</item>
  3.     <!-- All customizations that are NOT specific to a particular API-level can go here. -->
  4. </style>

2、.sub

  1. <style name="AppBaseTheme.sub">
  2.      <item name="android:windowNoTitle">true</item>
  3.     <!-- All customizations that are NOT specific to a particular API-level can go here. -->
  4. </style>

 

6、国际化

    国际化一般改变的就是语言和图片之类的,我么需要做的就是在res下新建一个后缀名是对应国家语言简写的drawable文件夹和value文件夹

    比如:

    技术分享

    必须保证新建的文件夹下的文件的名字与原对应的文件夹下的文件的名字是相同的

    文件内的内容也只是改改每个节点内的文本,其他的不变

    当系统的语言改变时,app就会根据系统语言去对应的文件夹里找资源

7、帧动画

  1. <animation-list xmlns:android="http://schemas.android.com/apk/res/android">
  2.     <item android:drawable="@drawable/logo1"
  3.           android:duration="150"></item>
  4.     <item android:drawable="@drawable/logo2"
  5.         android:duration="150"></item>
  6.     <item android:drawable="@drawable/logo3"
  7.         android:duration="150"></item>
  8. </animation-list>

这种动画只能放在drawable文件夹下

 

 

Day02