18乐学成语

来源:互联网 发布:网络视频服务器的作用 编辑:程序博客网 时间:2024/05/16 18:47

strings.xml页面:主页面

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <string name="app_name">happyidiom</string>  
  5.     <string name="action_settings">Settings</string>  
  6.   
  7.     <string name="title_study">学习</string>  
  8.     <string name="title_search">搜搜</string>  
  9.     <string name="title_game">游戏</string>  
  10.     <string name="title_save">收藏</string>  
  11.     <string name="title_help">帮助</string>  
  12.     <string name="animal">动物类</string>  
  13.       
  14.     <string-array name="category">  
  15.         <item>动物类</item>  
  16.         <item>自然类</item>  
  17.         <item>人物类</item>  
  18.         <item>季节类</item>  
  19.         <item>数字类</item>  
  20.         <item>寓言类</item>  
  21.         <item>其它类</item>  
  22.     </string-array>  
  23. </resources>  


在res目录下新建anim文件夹,存放两个动画页面,作为ListView内容显示时的动画效果。
anim_listview.xml页面:
[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <alpha xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:duration="1000"  
  4.     android:fromAlpha="0.0"  
  5.     android:toAlpha="1.0">  
  6. </alpha>  
anim_layout_listview.xml页面:
[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:animation="@anim/anim_listview"  
  4.     android:animationOrder="random"  
  5.     android:delay="0.2">  
  6. </layoutAnimation>  


layout文件夹下新建category_item.xml页面,作为显示成语类别ListView的子项目。
category_item.xml页面:
[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- “学习”中ListView的子布局 -->  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:padding="10dp"  
  7.     android:orientation="horizontal" >  
  8.       
  9.     <ImageView  
  10.         android:id="@+id/category_image"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:src="@drawable/category_animal"/>  
  14.   
  15.     <TextView  
  16.         android:id="@+id/category_name"  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:text="@string/animal"  
  20.         android:gravity="center"  
  21.         android:textColor="#000000"  
  22.         android:textAppearance="?android:attr/textAppearanceLarge"/>  
  23. </LinearLayout>  


新建animal_item.xml页面,作为显示详细成语ListView的子项目。
animal_item.xml页面:
[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- 成语列表布局 -->  
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:descendantFocusability="blocksDescendants"  
  7.     android:padding="10dp" >  
  8.     <!-- android:descendantFocusability="blocksDescendants"使ListView中的子控件ImageButton失去焦点 -->  
  9.     <TextView  
  10.         android:id="@+id/tvName"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_alignParentLeft="true"  
  14.         android:layout_alignParentTop="true"  
  15.         android:gravity="center"  
  16.         android:textColor="#000000"  
  17.         android:text="助人为乐"  
  18.         android:textAppearance="?android:attr/textAppearanceLarge"/>  
  19.   
  20.     <ImageButton  
  21.         android:id="@+id/btnSave"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:background="@null"  
  25.         android:layout_alignParentRight="true"  
  26.         android:layout_alignTop="@+id/tvName"  
  27.         android:src="@drawable/btnsave"/>  
  28. </RelativeLayout>  


新建dialog_info.xl页面,用于显示点击成语显示成语解释等信息的对话框页面。
dialog_info.xml页面:
[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <!-- 对话框布局 -->  
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.     <LinearLayout   
  6.         android:layout_width="match_parent"  
  7.         android:layout_height="match_parent"  
  8.         android:baselineAligned="@drawable/bg_ling"  
  9.         android:orientation="vertical">  
  10.         <TextView   
  11.             android:id="@+id/tvIdiomInfo"  
  12.             android:layout_width="match_parent"  
  13.             android:layout_height="wrap_content"  
  14.             android:text="Medium Text"  
  15.             android:textAppearance="?android:attr/textAppearanceMedium"/>  
  16.     </LinearLayout>  
  17. </ScrollView>  


新建android页面StudyActivity.java页面和activity_study.xml页面,activity_study.xml页面存放成语类别的ListView。
activity_study.xml页面:
[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <RelativeLayout 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:background="@drawable/bg_ling"  
  6.     tools:context=".StudyActivity" >  
  7.   
  8.     <ListView  
  9.         android:id="@+id/lvCategories"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:listSelector="#00000000"  
  13.         android:layoutAnimation="@anim/anim_layout_listview"  
  14.         android:layout_alignParentLeft="true"  
  15.         android:layout_alignParentTop="true" >  
  16.     </ListView>  
  17.   
  18. </RelativeLayout>  


10.新建android页面StudyAnimalActivity.java页面和activity_animal.xml页面,activity_animal.xml页面存放具体成语的ListView。
activity_anmal.xml页面:
[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  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:background="@drawable/bg_animal"  
  6.     android:orientation="vertical" >  
  7.     <ListView   
  8.         android:id="@+id/lvAnimalList"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:layoutAnimation="@anim/anim_layout_listview"  
  12.         android:listSelector="#00000000">  
  13.           
  14.     </ListView>  
  15.   
  16. </LinearLayout>  


activity_main.xml页面放置TabHost控件,用于标题的选择。
activity_main.xml页面:
[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <RelativeLayout 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.     tools:context=".MainActivity" >  
  6.   
  7.     <TabHost  
  8.         android:id="@android:id/tabhost"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true" >  
  13.   
  14.         <LinearLayout  
  15.             android:layout_width="match_parent"  
  16.             android:layout_height="match_parent"  
  17.             android:orientation="vertical" >  
  18.   
  19.             <TabWidget  
  20.                 android:id="@android:id/tabs"  
  21.                 android:layout_width="match_parent"  
  22.                 android:layout_height="wrap_content" />  
  23.   
  24.             <FrameLayout  
  25.                 android:id="@android:id/tabcontent"  
  26.                 android:layout_width="match_parent"  
  27.                 android:layout_height="match_parent" >  
  28.   
  29.                 <LinearLayout  
  30.                     android:id="@+id/tab1"  
  31.                     android:layout_width="match_parent"  
  32.                     android:layout_height="match_parent"  
  33.                     android:orientation="vertical" >  
  34.                 </LinearLayout>  
  35.   
  36.                 <LinearLayout  
  37.                     android:id="@+id/tab2"  
  38.                     android:layout_width="match_parent"  
  39.                     android:layout_height="match_parent"  
  40.                     android:orientation="vertical" >  
  41.                 </LinearLayout>  
  42.   
  43.                 <LinearLayout  
  44.                     android:id="@+id/tab3"  
  45.                     android:layout_width="match_parent"  
  46.                     android:layout_height="match_parent"  
  47.                     android:orientation="vertical" >  
  48.                 </LinearLayout>  
  49.             </FrameLayout>  
  50.         </LinearLayout>  
  51.     </TabHost>  
  52.   
  53. </RelativeLayout>  


entity包下新建Category.java类,作为成语类别的实体类。
Category.java页面:
[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package cn.edu.bztc.happyidiom.entity;  
  2.   
  3. public class Category {  
  4.     private String name;  
  5.     private int imageId;  
  6.     public Category(String name, int imageId) {  
  7.         super();  
  8.         this.name = name;  
  9.         this.imageId = imageId;  
  10.     }  
  11.     public String getName() {  
  12.         return name;  
  13.     }  
  14.     public int getImageId() {  
  15.         return imageId;  
  16.     }  
  17. }  


entity包下新建Animal.java类,作为成语具体信息的实体类。
Animal.java页面:
[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package cn.edu.bztc.happyidiom.entity;  
  2.   
  3. public class Animal {  
  4.     private int id;  
  5.     private String name;//成语名称  
  6.     private String pronounce;//成语发音  
  7.     private String explain;//成语解释  
  8.     private String antonym;//反义词  
  9.     private String homoionym;//同义词  
  10.     private String derivation;//源自  
  11.     private String examples;//例子  
  12.     public int getId() {  
  13.         return id;  
  14.     }  
  15.     public void setId(int id) {  
  16.         this.id = id;  
  17.     }  
  18.     public String getName() {  
  19.         return name;  
  20.     }  
  21.     public void setName(String name) {  
  22.         this.name = name;  
  23.     }  
  24.     public String getPronounce() {  
  25.         return pronounce;  
  26.     }  
  27.     public void setPronounce(String pronounce) {  
  28.         this.pronounce = pronounce;  
  29.     }  
  30.     public String getExplain() {  
  31.         return explain;  
  32.     }  
  33.     public void setExplain(String explain) {  
  34.         this.explain = explain;  
  35.     }  
  36.     public String getAntonym() {  
  37.         return antonym;  
  38.     }  
  39.     public void setAntonym(String antonym) {  
  40.         this.antonym = antonym;  
  41.     }  
  42.     public String getHomoionym() {  
  43.         return homoionym;  
  44.     }  
  45.     public void setHomoionym(String homoionym) {  
  46.         this.homoionym = homoionym;  
  47.     }  
  48.     public String getDerivation() {  
  49.         return derivation;  
  50.     }  
  51.     public void setDerivation(String derivation) {  
  52.         this.derivation = derivation;  
  53.     }  
  54.     public String getExamples() {  
  55.         return examples;  
  56.     }  
  57.     public void setExamples(String examples) {  
  58.         this.examples = examples;  
  59.     }  
  60.       
  61. }  


db包下新建DBOpenHelper.java页面,用于将复制到raw文件夹下的数据库文件读取并保存到databases文件夹中。
DBOpenHelper.java页面:
[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package cn.edu.bztc.happyidiom.db;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8.   
  9. import cn.edu.bztc.happyidion.activity.R;  
  10.   
  11.   
  12. import android.content.Context;  
  13. import android.database.sqlite.SQLiteDatabase;  
  14. import android.os.Environment;  
  15. import android.util.Log;  
  16.   
  17. /*实现将数据库文件从raw目录拷贝到手机里存放数据库的位置*/  
  18. public class DBOpenHelper {  
  19.     private final int BUFFER_SIZE=400000;//缓冲区大小  
  20.     public static final String DB_NAME="idioms.db";//保存的数据库文件名  
  21.     public static final String PACKAGE_NAME="cn.edu.bztc.happyidion.activity";//应用的包名  
  22.     public static final String DB_PATH="/data"+Environment.getDataDirectory().getAbsolutePath()+"/"+PACKAGE_NAME+"/databases";//在手机里存放数据库的位置  
  23.     private Context context;  
  24.     public DBOpenHelper(Context context){  
  25.         this.context=context;  
  26.     }  
  27.     public SQLiteDatabase openDatabase(){  
  28.         try {  
  29.             File myDataPath=new File(DB_PATH);  
  30.             if(!myDataPath.exists()){  
  31.                 myDataPath.mkdirs();//如果没有这个目录则创建  
  32.             }  
  33.             String dbfile=myDataPath+"/"+DB_NAME;  
  34.             if(!(new File(dbfile).exists())){//判断数据库文件是否存在,如果不存在则执行导入,否则直接打开数据库  
  35.                 InputStream is=context.getResources().openRawResource(R.raw.idioms);  
  36.                 FileOutputStream fos=new FileOutputStream(dbfile);  
  37.                 byte[] buffer=new byte[BUFFER_SIZE];  
  38.                 int count=0;  
  39.                 while((count=is.read(buffer))>0){  
  40.                     fos.write(buffer,0,count);  
  41.                 }  
  42.                 fos.close();  
  43.                 is.close();  
  44.             }  
  45.             SQLiteDatabase db=SQLiteDatabase.openOrCreateDatabase(dbfile,null);  
  46.             return db;  
  47.         } catch (FileNotFoundException e) {  
  48.             Log.e("MainActivity","File not found");  
  49.             e.printStackTrace();  
  50.         }catch (IOException e) {  
  51.             Log.e("MainActivity","IO exception");  
  52.             e.printStackTrace();  
  53.         }  
  54.         return null;  
  55.     }  
  56. }  


test包下新建DBOpenHelperTest.java页面测试一下数据库是否处理成功。
DBOpenHelperTest.java页面:
[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package cn.edu.bztc.happyidiom.test;  
  2.   
  3. import cn.edu.bztc.happyidiom.db.DBOpenHelper;  
  4. import android.test.AndroidTestCase;  
  5.   
  6. public class DBOpenHelperTest extends AndroidTestCase{  
  7.     public void testDBCopy(){  
  8.         DBOpenHelper dbOpenHelper=new DBOpenHelper(getContext());  
  9.         dbOpenHelper.openDatabase();  
  10.     }  
  11. }  
0 0
原创粉丝点击