乐学成语(HappyIdiom)
来源:互联网 发布:俩字网络新兴词语 编辑:程序博客网 时间:2024/04/29 18:52
- package cn.edu.bztc.happyidiom.test;
- import java.util.List;
- import cn.edu.bztc.happyidiom.dao.AnimalDao;
- import cn.edu.bztc.happyidiom.entity.Animal;
- import android.test.AndroidTestCase;
- public class AnimalDaoTest extends AndroidTestCase{
- public void testGetAllAnimals(){
- AnimalDao animalDao=AnimalDao.getInstance(getContext());
- List<Animal> animals=animalDao.getAllAnimals();
- System.out.println(animals.size());
- for(Animal animal:animals){
- System.out.println(animal.getName());
- }
- }
- }
二、显示主界面
1、activity_main.xml布局,代码如下:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity" >
- <TabHost
- android:id="@android:id/tabhost"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TabWidget
- android:id="@android:id/tabs"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- </TabWidget>
- <FrameLayout
- android:id="@android:id/tabcontent"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1">
- <LinearLayout
- android:id="@+id/tab1"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- </LinearLayout>
- <LinearLayout
- android:id="@+id/tab2"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- </LinearLayout>
- <LinearLayout
- android:id="@+id/tab3"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- </LinearLayout>
- </FrameLayout>
- </LinearLayout>
- </TabHost>
- </RelativeLayout>
2、在res的values目录的strings.xml文件中定义所需的字符串。代码如下所示:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">HappyIdiom</string>
- <string name="action_settings">Settings</string>
- <string name="hello_world">Hello world!</string>
- <string name="title_activity_main">MainActivity</string>
- <string name="title_study">学习</string>
- <string name="title_search">搜搜</string>
- <string name="title_game">游戏</string>
- <string name="title_save">收藏</string>
- <string name="title_help">帮助</string>
- <string-array name="category">
- <item>动物类</item>
- <item>自然类</item>
- <item>人物类</item>
- <item>季节类</item>
- <item>数字类</item>
- <item>寓言类</item>
- <item>其他类</item>
- </string-array>
- <string name="title_activity_study">StudyActivity</string>
- <string name="title_activity_animal">AnimalActivity</string>
- </resources>
3、编写一个活动MainActivity,代码如下:
- package cn.edu.bztc.happyidiom.activity;
- import cn.edu.bztc.happyidiom.R;
- import cn.edu.bztc.happyidiom.R.layout;
- import cn.edu.bztc.happyidiom.R.menu;
- import android.os.Bundle;
- import android.app.TabActivity;
- import android.content.Intent;
- import android.view.Menu;
- import android.view.Window;
- import android.widget.TabHost;
- public class MainActivity extends TabActivity {
- private TabHost tabHost;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE); //取消标题栏
- setContentView(R.layout.activity_main);
- tabHost=getTabHost();
- //如果没有继承TabActivity时,通过该种方法加载启动tabHost
- //tabHost.setup();
- addTab("study",R.string.title_study,R.drawable.study,StudyActivity.class);
- addTab("search",R.string.title_search,R.drawable.search,StudyActivity.class);
- addTab("game",R.string.title_game,R.drawable.game,StudyActivity.class);
- addTab("save", R.string.title_save, R.drawable.save, StudyActivity.class);
- addTab("help", R.string.title_help, R.drawable.help, StudyActivity.class);
- }
- /*private TabHost getTabHost() {
- // TODO Auto-generated method stub
- //获取TabHost对象
- TabHost tabHost=(TabHost) findViewById(R.id.tabhost);
- return tabHost;
- }*/
- private void addTab(String tag,int title_introduction,int title_icon,Class ActivityClass){
- tabHost.addTab(tabHost.newTabSpec(tag).setIndicator(getString(title_introduction),getResources().getDrawable(title_icon)).setContent(new Intent(this,ActivityClass)));
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
【注意】:(1)取标题栏的方法,一定要位于setContentView()方法之前。
(2)如果MainActivity继承的是Activity,没有继承TabActivity时,
则要通过tabHost.setup();加载启动tabHost
运行程序如图所示:
如果运行结果指示器上只显示出了文字,图片为显示出来,可以通过修改主题来实现。
如设置activity的theme为:
- android:theme="@android:style/Theme.Black.NoTitleBar"
三、显示学习列表
1、定义一个实体类,作为ListView适配器的适配类型。
在entity包下新建类Category,代码如下所示:
- package cn.edu.bztc.happyidiom.entity;
- public class Category {
- private String name;//类别名称
- private int imageId;//类别对应的图片
- public Category(String name, int imageId) {
- super();
- this.name = name;
- this.imageId = imageId;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getImageId() {
- return imageId;
- }
- public void setImageId(int imageId) {
- this.imageId = imageId;
- }
- }
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/bg_ling"
- tools:context=".StudyActivity" >
- <ListView
- android:id="@+id/lvCategories"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:listSelector="#00000000"
- android:layoutAnimation="@anim/anim_layout_listview"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true">
- </ListView>
- </RelativeLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:padding="10dp"
- android:orientation="horizontal" >
- <ImageView
- android:id="@+id/category_image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/category_animal" />
- <TextView
- android:id="@+id/category_name"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@array/category"
- android:gravity="center"
- android:textAppearance="?android:attr/textAppearanceLarge" />
- </LinearLayout>
4、在adapter包下创建一个自定义的适配器,新建类CategoryAdapter,代码如下:
- package cn.edu.bztc.happyidiom.adapter;
- import java.util.List;
- import cn.edu.bztc.happyidiom.R;
- import cn.edu.bztc.happyidiom.entity.Category;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ArrayAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
- public class CategoryAdapter extends ArrayAdapter<Category>{
- private int resourceId;
- public CategoryAdapter(Context context, int resource, List<Category> objects) {
- super(context, resource, objects);
- // TODO Auto-generated constructor stub
- resourceId=resource;
- }
- public View getView(int position,View convertView,ViewGroup parent){
- Category category=getItem(position);//获取当前项的Category实例
- //View view=LayoutInflater.from(getContext()).inflate(resourceId, null);
- //ImageView cateoryImage=(ImageView) view.findViewById(R.id.category_image);
- //TextView categoryName=(TextView) view.findViewById(R.id.category_name);
- //cateoryImage.setImageResource(category.getImageId());
- //categoryName.setText(category.getName());
- View view;
- ViewHolder viewHolder;
- if(convertView==null){
- view=LayoutInflater.from(getContext()).inflate(resourceId, null);
- viewHolder=new ViewHolder();
- viewHolder.categoryImage=(ImageView) view.findViewById(R.id.category_image);
- viewHolder.categoryName=(TextView) view.findViewById(R.id.category_name);
- view.setTag(viewHolder);//将ViewHolder存储在View中
- }else{
- view = convertView;
- viewHolder=(ViewHolder) view.getTag();//重新获取ViewHolder
- }
- viewHolder.categoryImage.setImageResource(category.getImageId());
- viewHolder.categoryName.setText(category.getName());
- return view;
- }
- class ViewHolder{
- ImageView categoryImage;
- TextView categoryName;
- }
- }
新增了一个内部类ViewHolder,用于对控件的实例进行缓存。当convertView为空时,创建一个ViewHolder对象,并将控件的实例都存放在ViewHolder里,然后调用View的setTag()方法,将ViewHolder对象存储在View中。当convertView不为空时则调用View的setTag()方法,把ViewHolder重新取出。这样所有控件的实例都缓存在了ViewHolder里,就美哟必要每次都通过findViewById()方法来获取控件示例了。
5、在activity包下新建StudyActivity,代码如下:
- package cn.edu.bztc.happyidiom.activity;
- import java.util.ArrayList;
- import java.util.List;
- import cn.edu.bztc.happyidiom.R;
- import cn.edu.bztc.happyidiom.R.layout;
- import cn.edu.bztc.happyidiom.R.menu;
- import cn.edu.bztc.happyidiom.adapter.CategoryAdapter;
- import cn.edu.bztc.happyidiom.entity.Category;
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.res.Resources;
- import android.view.Menu;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.ListView;
- import android.widget.Toast;
- public class StudyActivity extends Activity {
- private List<Category> categoryList;
- private String[] category_names;
- private int[] category_images;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_study);
- initCategories();//初始化类别
- CategoryAdapter adapter=new CategoryAdapter(this, R.layout.category_item, categoryList);
- ListView listView=(ListView) findViewById(R.id.lvCategories);
- listView.setAdapter(adapter);
- listView.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> adapterView, View view, int position,
- long id) {
- // TODO Auto-generated method stub
- switch(position){
- case 0:
- Intent intent=new Intent(StudyActivity.this,AnimalActivity.class);
- startActivity(intent);
- break;
- default:
- break;
- }
- }
- });
- }
- private void initCategories() {
- // TODO Auto-generated method stub
- categoryList=new ArrayList<Category>();
- Resources resources=getResources();
- category_names=resources.getStringArray(R.array.category);
- category_images=new int[]{R.drawable.category_animal,R.drawable.category_nature,R.drawable.category_human,R.drawable.category_season,R.drawable.category_number,R.drawable.category_fable,R.drawable.category_other};
- for(int i=0;i<category_names.length;i++){
- categoryList.add(new Category(category_names[i],category_images[i]));
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.study, menu);
- return true;
- }
- }
使用了setOnItemClickListener()方法来为ListView注册了一个监听器,当用户点击了ListView中的任何一个子项时就会回调onItemClick()方法,在这个方法中可以通过position参数判断出用户点击的是哪一个子项,然后获取到相应的类别,并通过Toast将类别的名字显示出来。
运行程序如图所示:
在点击每一项时会出现橙黄色的背景色,如果去点背景色,则在activity_study.xml文件中加一句话:
- <ListView
- android:id="@+id/lvCategories"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:listSelector="#00000000"<span style="white-space:pre"> </span>//去掉橙黄色的背景色
- android:layoutAnimation="@anim/anim_layout_listview"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true">
- </ListView>
6、界面载入的过程有点僵硬,为界面增加淡入淡出的动画效果。
在res目录下新建anim目录,在下面创建anim_listview.xml文件,代码如下所示:
- <?xml version="1.0" encoding="utf-8"?>
- <alpha xmlns:android="http://schemas.android.com/apk/res/android"
- android:duration="1000"
- android:fromAlpha="0.0"
- android:toAlpha="1.0">
- </alpha>
创建anim_layout_listview.xml文件,代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
- android:animation="@anim/anim_listview"
- android:animationOrder="random"
- android:delay="0.2">
- </layoutAnimation>
四、显示所有动物类成语的列表
1、新建activity_animal.xml文件,主要添加了一个ListView控件,代码如下所示:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".AnimalActivity" >
- <ListView
- android:id="@+id/lvAnimalList"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layoutAnimation="@anim/anim_layout_listview"
- android:listSelector="#00000000">
- </ListView>
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:padding="10dp" >
- <TextView
- android:id="@+id/tvName"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:gravity="center"
- android:text="助人为乐"
- android:textAppearance="?android:attr/textAppearanceLarge" />
- <ImageButton
- android:id="@+id/btnSave"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@null"
- android:layout_alignParentRight="true"
- android:layout_alignTop="@+id/tvName"
- android:src="@drawable/btnsave" />
- </RelativeLayout>
3、新建类AnimalAdapter,代码如下所示:
显示弹窗:
- <span style="white-space:pre"> </span>viewHolder.btnSave.setFocusable(false);
- viewHolder.btnSave.setFocusableInTouchMode(false);
- package cn.edu.bztc.happyidiom.adapter;
- import java.util.List;
- import cn.edu.bztc.happyidiom.R;
- import cn.edu.bztc.happyidiom.adapter.CategoryAdapter.ViewHolder;
- import cn.edu.bztc.happyidiom.entity.Animal;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.widget.ArrayAdapter;
- import android.widget.ImageButton;
- import android.widget.TextView;
- import android.widget.Toast;
- public class AnimalAdapter extends ArrayAdapter<Animal>{
- private int resourceId;
- private Context context;
- public AnimalAdapter(Context context, int resource, List<Animal> objects) {
- super(context, resource, objects);
- // TODO Auto-generated constructor stub
- this.context=context;
- resourceId=resource;
- }
- public View getView(int position,View convertView,ViewGroup parent){
- final Animal animal=getItem(position);//获取当前项的Animal实例
- View view;
- ViewHolder viewHolder;
- if(convertView==null){
- view=LayoutInflater.from(getContext()).inflate(resourceId, null);
- viewHolder=new ViewHolder();
- viewHolder.tvName=(TextView) view.findViewById(R.id.tvName);
- viewHolder.btnSave=(ImageButton) view.findViewById(R.id.btnSave);
- viewHolder.btnSave.setFocusable(false);
- viewHolder.btnSave.setFocusableInTouchMode(false);
- viewHolder.btnSave.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- // TODO Auto-generated method stub
- Toast.makeText(context, "你要收藏"+animal.getName()+"吗", Toast.LENGTH_SHORT).show();
- }
- });
- view.setTag(viewHolder);//将ViewHolder存储在View中
- }else{
- view=convertView;
- viewHolder=(ViewHolder) view.getTag();//重新获取ViewHolder
- }
- viewHolder.tvName.setText(animal.getName());
- return view;
- }
- class ViewHolder{
- TextView tvName;
- ImageButton btnSave;
- }
- }
- package cn.edu.bztc.happyidiom.activity;
- import java.util.List;
- import cn.edu.bztc.happyidiom.R;
- import cn.edu.bztc.happyidiom.R.layout;
- import cn.edu.bztc.happyidiom.R.menu;
- import cn.edu.bztc.happyidiom.adapter.AnimalAdapter;
- import cn.edu.bztc.happyidiom.dao.AnimalDao;
- import cn.edu.bztc.happyidiom.entity.Animal;
- import cn.edu.bztc.happyidiom.util.DialogUtil;
- import android.os.Bundle;
- import android.app.Activity;
- import android.app.Dialog;
- import android.view.Menu;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.ListView;
- public class AnimalActivity extends Activity {
- private List<Animal> animalList;
- private AnimalDao animalDao;
- private ListView lvAnimalList;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_animal);
- initAnimals();
- lvAnimalList=(ListView) findViewById(R.id.lvAnimalList);
- AnimalAdapter animalAdapter=new AnimalAdapter(this, R.layout.animal_item, animalList);
- lvAnimalList.setAdapter(animalAdapter);
- lvAnimalList.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> adapterView, View view, int position,
- long id) {
- // TODO Auto-generated method stub
- Animal animal=animalList.get(position);
- String result=animal.getName()+"\n"+animal.getPronounce()+"\n【解释】:"+animal.getExplain()+"\n【近义词】:"+animal.getHomoionym()+"\n【反义词】:"+animal.getAntonym()+"\n【来源】:"+animal.getDerivation()+"\n【实例】:"+animal.getExamples();
- DialogUtil.showDialog(result, AnimalActivity.this);
- }
- });
- }
- private void initAnimals(){
- animalDao=AnimalDao.getInstance(this);
- animalList=animalDao.getAllAnimals();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.animal, menu);
- return true;
- }
- }
DialogUtil.showDialog()方法是自定义的方法。在util包下新建DialogUtil类,如下所示:
- package cn.edu.bztc.happyidiom.util;
- import cn.edu.bztc.happyidiom.R;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.TextView;
- public class DialogUtil {
- public static void showDialog(String result,Context context){
- AlertDialog.Builder builder=new AlertDialog.Builder(context);
- LayoutInflater layoutInflater=LayoutInflater.from(context);
- View view=layoutInflater.inflate(R.layout.dialog_info, null);
- builder.setView(view);
- TextView tvldiomlnfo=(TextView) view.findViewById(R.id.tvldiomlnfo);
- tvldiomlnfo.setText(result);
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- dialog.dismiss();
- }
- });
- builder.create().show();
- }
- }
运行如图所示:
五、显示每条成语的详细信息
1、新建布局文件dialog_info.xml,代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/bg_ling"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/tvldiomlnfo"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Medium Text"
- android:textAppearance="?android:attr/textAppearanceMedium" />
- </LinearLayout>
- </ScrollView>
运行如图所示:
六、修改图标和名称
修改AndroidManifest.xml中的代码,如下所示:
- <application
- android:allowBackup="true"
- android:icon="@drawable/logo"<span style="white-space:pre"> </span>//修改图标
- android:label="@string/app_name"
- android:theme="@android:style/Theme.Black.NoTitleBar" >
- <uses-library android:name="android.test.runner" />
- </application>
- 乐学成语(HappyIdiom)
- 乐学成语(HappyIdiom)
- 项目乐学成语HappyIdiom
- 乐学成语(1)
- 乐学成语(2)
- 乐学成语(3)
- 乐学成语
- 乐学成语
- 乐学成语
- 乐学成语2
- 乐学成语
- 乐学成语
- 乐学成语案例
- Android---乐学成语
- 乐学成语
- 乐学成语
- 乐学成语2
- 17乐学成语
- JAVA基础知识点梳理二:运算符
- Maven默认的JDK版本
- Service使用技巧,一些不为人知的功能
- 快速排序
- 布局填充的另一种方法
- 乐学成语(HappyIdiom)
- 三十而立
- Thinking in Java 中闪耀的星星(一)
- 红黑树的定义与性质
- jsp文件的存放位置
- Android自定义控件-完全自定义(继承View ,ViewGroup)
- RGB颜色表
- LibGdx文档译读(三)Audio接口
- linux chmod命令参数及用法详解