andriod之乐学成语

2024-03-24 20:10
文章标签 andriod 成语 之乐学

本文主要是介绍andriod之乐学成语,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

效果图大笑








第一步:建立数据库
加载数据数据库到项目中来,在res目录下建立一个raw文件夹, 

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

测试一下是否成功
在AndroidManifest.xml中建立单元测试环境
[html]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="cn.deu.bztc.happyidiom.activity"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="17"  
  9.         android:targetSdkVersion="17" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/logo"  
  14.         android:label="@string/app_name"  
  15.         android:screenOrientation="portrait"  
  16.         android:theme="@android:style/Theme.Translucent.NoTitleBar" >  
  17.        <span style="color:#ff0000;"> <uses-library android:name="android.test.runner" /></span>  
  18.   
  19.         <activity  
  20.             android:name="cn.deu.bztc.happyidiom.activity.MainActivity"  
  21.             android:label="@string/app_name" >  
  22.             <intent-filter>  
  23.                 <action android:name="android.intent.action.MAIN" />  
  24.   
  25.                 <category android:name="android.intent.category.LAUNCHER" />  
  26.             </intent-filter>  
  27.         </activity>  
  28.         <activity  
  29.             android:name="cn.deu.bztc.happyidiom.activity.StudyAnimalActivity"  
  30.             android:label="@string/app_name" >  
  31.         </activity>  
  32.         <activity  
  33.             android:name="cn.deu.bztc.happyidiom.util.DialogUtil"  
  34.             android:label="@string/app_name" >  
  35.         </activity>  
  36.         <activity  
  37.             android:name="cn.deu.bztc.happyidiom.activity.StudyActivity"  
  38.             android:label="@string/app_name" >  
  39.         </activity>  
  40.         <activity  
  41.             android:name="com.tangsci.android.example.ttsdemo.TtsDemoActivity"  
  42.             android:label="@string/app_name" >  
  43.         </activity>  
  44.     </application>  
  45.   
  46.     <span style="color:#ff0000;"><instrumentation  
  47.         android:name="android.test.InstrumentationTestRunner"  
  48.         android:targetPackage="cn.deu.bztc.happyidiom.activity" >  
  49.     </instrumentation></span>  
  50.   
  51. </manifest>  

测试一下是否成功:
在test包下建立DBOpenHelpTest.java
[java]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. package cn.deu.bztc.happyidiom.test;  
  2.   
  3. import java.util.List;  
  4.   
  5. import cn.deu.bztc.happyidiom.dao.AnimalDao;  
  6. import cn.deu.bztc.happyidiom.db.DBOpenHelper;  
  7. import cn.deu.bztc.happyidiom.entity.Animal;  
  8. import android.test.AndroidTestCase;  
  9.   
  10. public class DBOpenHelpTest extends AndroidTestCase {  
  11.     public void testDBCOpy(){  
  12.         DBOpenHelper  dbopenHelper=new DBOpenHelper(getContext());  
  13.         dbopenHelper.openDatabase();  
  14.     }  
  15.     public void testGetAllAnimals(){  
  16.         AnimalDao animalDao=AnimalDao.getInstance(getContext());  
  17.         List<Animal> animals=animalDao.getAllAnimals();  
  18.         System.out.println(animals.size());  
  19.         for(Animal animal:animals){  
  20.             System.out.println(animal.getName());  
  21.         }  
  22.     }  

cn.deu.bztc.happyidiom.entity建立实体类  
Animal.java
[java]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. package cn.deu.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. }  
然后在数据库管理层 (Dao 层)建立操作类AnimalDao.java 大笑
[java]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. package cn.deu.bztc.happyidiom.dao;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import cn.deu.bztc.happyidiom.db.DBOpenHelper;  
  7. import cn.deu.bztc.happyidiom.entity.Animal;  
  8. import android.content.Context;  
  9. import android.database.Cursor;  
  10. import android.database.sqlite.SQLiteDatabase;  
  11.   
  12. public class AnimalDao {  
  13.     private static AnimalDao animalDao;  
  14.     private static SQLiteDatabase db;  
  15.     /** 
  16.      * 将构造方法私有化 
  17.      */  
  18.     public AnimalDao(Context  context) {  
  19.         DBOpenHelper dbHelper=new DBOpenHelper(context);  
  20.         db=dbHelper.openDatabase();  
  21.     }  
  22.     /** 
  23.      * 获取AnimalDao实例 
  24.      */  
  25.     public synchronized static AnimalDao getInstance(Context context){  
  26.         if(animalDao==null){  
  27.             animalDao=new AnimalDao(context);  
  28.         }  
  29.         return animalDao;  
  30.     }  
  31.     /** 
  32.      * 从数据库读取所有的动物类成语 
  33.      */   
  34.     public static  List<Animal> getAllAnimals(){  
  35.         List<Animal> list=new ArrayList<Animal>();  
  36.         Cursor cursor=db.query("animal"nullnullnullnull,null,null);  
  37.         if(cursor.moveToFirst()){  
  38.             do{  
  39.                 Animal animal=new Animal();  
  40.                 animal.setId(cursor.getInt(cursor.getColumnIndex("_id")));  
  41.                 animal.setName(cursor.getString(cursor.getColumnIndex("name")));  
  42.                 animal.setPronounce(cursor.getString(cursor.getColumnIndex("pronounce")));  
  43.                 animal.setAntonym(cursor.getString(cursor.getColumnIndex("antonym")));  
  44.                 animal.setHomoionym(cursor.getString(cursor.getColumnIndex("homoionym")));  
  45.                 animal.setDerivation(cursor.getString(cursor.getColumnIndex("derivation")));  
  46.                 animal.setExamples(cursor.getString(cursor.getColumnIndex("examples")));  
  47.                 list.add(animal);  
  48.             }while(cursor.moveToNext());  
  49.         }  
  50.         return list;  
  51.     }  
  52. }  
下面进行单元测试
在test测试类下 加入
[java]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. public void testGetAllAnimals(){  
  2.         AnimalDao animalDao=AnimalDao.getInstance(getContext());  
  3.         List<Animal> animals=animalDao.getAllAnimals();  
  4.         System.out.println(animals.size());  
  5.         for(Animal animal:animals){  
  6.             System.out.println(animal.getName());  
  7.         }  


测试成功后,实现功能

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.     android:background="@drawable/bg_animal" >  
  7.   
  8.     <TabHost  
  9.         android:id="@android:id/tabhost"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="match_parent"  
  12.         android:layout_alignParentLeft="true"  
  13.         android:layout_alignParentTop="true" >  
  14.   
  15.         <LinearLayout  
  16.             android:layout_width="match_parent"  
  17.             android:layout_height="match_parent"  
  18.             android:orientation="vertical" >  
  19.   
  20.             <FrameLayout  
  21.                 android:id="@android:id/tabcontent"  
  22.                 android:layout_width="match_parent"  
  23.                 android:layout_height="match_parent"  
  24.                 android:layout_weight="1" >  
  25.   
  26.                 <LinearLayout  
  27.                     android:id="@+id/tab2"  
  28.                     android:layout_width="match_parent"  
  29.                     android:layout_height="match_parent"  
  30.                     android:orientation="vertical" >  
  31.                 </LinearLayout>  
  32.   
  33.                 <LinearLayout  
  34.                     android:id="@+id/tab3"  
  35.                     android:layout_width="match_parent"  
  36.                     android:layout_height="match_parent"  
  37.                     android:orientation="vertical" >  
  38.                 </LinearLayout>  
  39.             </FrameLayout>  
  40.   
  41.             <TabWidget  
  42.                 android:id="@android:id/tabs"  
  43.                 android:layout_width="match_parent"  
  44.                 android:layout_height="wrap_content" >  
  45.             </TabWidget>  
  46.         </LinearLayout>  
  47.     </TabHost>  
  48.   
  49. </RelativeLayout>  

[html]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. <string-array name="category">  
  2.        <item>动物类</item>  
  3.        <item>自然类</item>  
  4.        <item>人物类</item>  
  5.        <item>季节类</item>  
  6.        <item>数字类</item>  
  7.        <item>寓言类</item>  
  8.        <item>其他类</item>  
  9.    </string-array>  
接下来cn.deu.bztc.happyidiom.activity中建立MainActivity.java
[java]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. package cn.deu.bztc.happyidiom.activity;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.app.TabActivity;  
  6. import android.content.Intent;  
  7. import android.view.Menu;  
  8. import android.view.Window;  
  9. import android.widget.TabHost;  
  10.   
  11. public class MainActivity extends TabActivity {  
  12.     private TabHost  tabHost;  
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         requestWindowFeature(Window.FEATURE_NO_TITLE);//取消标题栏  
  17.         setContentView(R.layout.activity_main);  
  18.         tabHost=getTabHost();  
  19.         addTab("study", R.string.title_study, R.drawable.study,StudyActivity.class);  
  20.         addTab("search", R.string.title_search, R.drawable.search, StudyActivity.class);  
  21.         addTab("study", R.string.title_game, R.drawable.game, StudyActivity.class);  
  22.         addTab("save", R.string.title_save, R.drawable.save, StudyActivity.class);  
  23.         addTab("help", R.string.title_help, R.drawable.search, StudyActivity.class);  
  24.     }  
  25.     private void addTab(String tag,int title_introduction,int title_icon,Class ActivityClass){  
  26.         tabHost.addTab(tabHost.newTabSpec(tag)  
  27.                 .setIndicator(getString(title_introduction),  
  28.                         getResources().getDrawable(title_icon)).setContent(new Intent(this,ActivityClass)));  
  29.     }  
  30.       
  31.     @Override  
  32.     public boolean onCreateOptionsMenu(Menu menu) {  
  33.         // Inflate the menu; this adds items to the action bar if it is present.  
  34.         getMenuInflater().inflate(R.menu.main, menu);  
  35.         return true;  
  36.     }  
  37.   

这篇关于andriod之乐学成语的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/842649

相关文章

3600关成语填字APP游戏ACCESS\EXCEL数据库

成语类的APP游戏在最近一两年内非常的火爆,其主要原因是几乎所有中国人都能够冲个几十上百关,学习和趣味共享。看图猜成语类的数据之前已经弄到过很多,今天这份成语填字的倒是头一份。 该数据做成的APP效果如下: 数据以\符号分隔,每个部分的意思为:[行,列,字,是否显示] 数据展示如下: 数据提供EXCEL表格文件、CSV、ACCESS数据库文件、SQLite3数据库文件,如有

Andriod打包的过程(搬运)

原文章地址: http://hanhailong.com/2016/04/02/apk%25E6%2589%2593%25E5%258C%2585%25E6%25B5%2581%25E7%25A8%258B%25E6%25A2%25B3%25E7%2590%2586/ 简介 一些初学Android的开发者可能只知道写完项目,然后点击AndroidStudio中的run按钮就可以把项目运行到

android学习笔记——andriod项目文件夹结果

命令 : android create project -n 项目名称 -t 项目对应的android平台 -p 项目的保存路径 -k 项目的包名 -a 指定Activity的名称 生成项目的目录 libsres drawable-ldpi、 drawable-mdpi、 drawable-hdpi、 drawable-xhdpi用于存放不同分辨率的图片(低、中、高、超高)layout存放

andriod学习笔记——安卓应用程序生命周期

进程创建到消亡的过程 安卓程序不能主宰自己的命运,有系统决定 进程优先性: 按照应用程序的组件以及组件的运行状态将所有进程重要性程度分为五个级别: 一、前台进程:在频幕最前端显示、并和用户正在交互的Activity进程,或者这个进程正在运行BroadcastReceiver(广播接 受)只有少数,只有在内存极少时才会终止 判断依据:1、进程正在最前端运行一个和用

andriod学习笔记——andriod框架

应用程序层 - 短信、电话等都是Java开发的应用程序 应用程序框架层 - 简化组件重用,可继承拓展  是应用程序层的基础 活动管理器  管理各个应用程序生命周期以及导航回退功能 窗口管理器  管理所有的窗口程序 内容提供器 Content Providers  不同应用程序之间存取共享数据 视图系统      构建应用程序的基本组件 通告管理器 Notification

Andriod Studio-gradle build时间过长解决方案

在创建好的项目文件夹内,找到build.gradle文件,使用notepad打开文件,将repositories代码原内容注释,改为以下部分代码: // Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript {repositories

收集成语单词

公共成语表 个人成语表。

【七合一】字典词典成语古诗词造句英语单词文库

帝国CMS7.5 UTF-8 系统开源,不限域名 采用静态+伪静态(会缓存静态文件) 一款7合一的字词句诗典籍模板,包含字典、词典、成语、名句、诗词、古籍、英语、作文、等等。是一款养站神器。 作文范文,作文范文可生成word文档下载能自由的设置每一个文章收费还是免费(默认设置免费) 因数据量庞大,基础数据约有100万+页面,所以硬盘最低50G,如果您没有云主机或者VPS将无法安装。 【七

Andriod点击按钮响应方法

点击按钮响应有许多方法,比如下面这种: 布局layout.xml中定义一个id为button的按钮<Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"/> 在活动的java文件中按钮响应代码:Button btn = findViewByI

Andriod Reader 简介

所有读者的基类。 读者是一个意味着读取数据从源在一个字符明智的方式。 一些读者也支持标记一个位置在输入并返回到这个位置后。 直接已知子类: BufferedReader, CharArrayReader, FilterReader, InputStreamReader, PipedReader, StringReader 所有已实现的接口: Closeable, Readable