乐学成语

来源:互联网 发布:手机淘宝怎么发买家秀 编辑:程序博客网 时间:2024/04/29 18:12

1.MainActivity.java

package com.example.lexueidiom;

import android.os.Bundle;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TextView;

 public class MainActivity extends TabActivity {
  TabHost mTabHost;
  private TextView tv_show;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   getWindow().requestFeature(Window.FEATURE_CUSTOM_TITLE);
   setContentView(R.layout.activity_main);
   Intent study_intent = new Intent();
   study_intent.setClass(MainActivity.this, study_Activity.class);
   mTabHost = getTabHost();
   TabHost.TabSpec Tab1 = mTabHost.newTabSpec("tab1");
   Tab1.setIndicator("学习",
     getResources().getDrawable(R.drawable.study_study)).setContent(
     study_intent);
   mTabHost.addTab(Tab1);
   TabHost.TabSpec Tab2 = mTabHost.newTabSpec("tab2");
   Tab2.setIndicator("搜搜",
     getResources().getDrawable(R.drawable.study_search))
     .setContent(R.id.activity_search);
   mTabHost.addTab(Tab2);
   TabHost.TabSpec Tab3 = mTabHost.newTabSpec("tab3");
   Tab3.setIndicator("游戏",
     getResources().getDrawable(R.drawable.study_game)).setContent(
     R.id.activity_game);
   mTabHost.addTab(Tab3);
   TabHost.TabSpec Tab4 = mTabHost.newTabSpec("tab4");
   Tab4.setIndicator("收藏",
     getResources().getDrawable(R.drawable.study_save)).setContent(
     R.id.activity_save);
   mTabHost.addTab(Tab4);
   TabHost.TabSpec Tab5 = mTabHost.newTabSpec("tab5");
   Tab5.setIndicator("帮助",
     getResources().getDrawable(R.drawable.study_help)).setContent(
     R.id.activity_help);
   mTabHost.addTab(Tab5);
   getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
   tv_show = (TextView) this.findViewById(R.id.tv_show);
   tv_show.append("一心一意" + " ");
   tv_show.append("三心二意" + " ");
   tv_show.append("有情有义" + " ");
   tv_show.append("一叶知秋" + " ");
   tv_show.append("风和日丽" + " ");
   tv_show.append("四平八稳" + " ");
   tv_show.append("一叶障目" + " ");
  }

  @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;
  }

 }

2.study_Activity.java

package com.example.lexueidiom;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;


public class study_Activity extends ListActivity {

 private ListView List;
 List<Map<String, Object>> data;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //setContentView(R.layout.activity_study);
  List = getListView();
  getData();
  setListAdapter(new SimpleAdapter(this, data, R.layout.list_study,
    new String[] { "image", "sort" }, new int[] { R.id.img_pre,
      R.id.text }));
 }

private void getData(){
  data = new ArrayList<Map<String, Object>>();
  int[] images=new int[]{R.drawable.study_animal,
    R.drawable.study_human,R.drawable.study_season,
    R.drawable.study_nature,R.drawable.study_number,
    R.drawable.study_fable,R.drawable.study_different,
    R.drawable.study_other,};
  int[] sorts=new int[]{R.string.animal,
    R.string.human,R.string.season,
    R.string.nature,R.string.number,
    R.string.fable,R.string.different,
    R.string.other};
  for (int i=0; i<images.length;i++){
   Map<String, Object> map = new HashMap<String, Object>();
   map.put("image", images[i]);
   map.put("sort", getString(sorts[i]));
   data.add(map);
  }
 }
 /* public List<Map<String, Object>> getData() {
  List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
  Map<String, Object> data01 = new HashMap<String, Object>();
  data01.put("image", R.drawable.study_animal);
  data01.put("sort", "动物类");
  data.add(data01);
  Map<String, Object> data02 = new HashMap<String, Object>();
  data02.put("image", R.drawable.study_human);
  data02.put("sort", "人物类");
  data.add(data02);
  Map<String, Object> data03 = new HashMap<String, Object>();
  data03.put("image", R.drawable.study_season);
  data03.put("sort", "季节类");
  data.add(data03);
  Map<String, Object> data04 = new HashMap<String, Object>();
  data04.put("image", R.drawable.study_nature);
  data04.put("sort", "自然类");
  data.add(data04);
  Map<String, Object> data05 = new HashMap<String, Object>();
  data05.put("image", R.drawable.study_number);
  data05.put("sort", "数字类");
  data.add(data05);
  Map<String, Object> data06 = new HashMap<String, Object>();
  data06.put("image", R.drawable.study_fable);
  data06.put("sort", "寓言类");
  data.add(data06);
  Map<String, Object> data07 = new HashMap<String, Object>();
  data07.put("image", R.drawable.study_different);
  data07.put("sort", "另类");
  data.add(data07);
  Map<String, Object> data08 = new HashMap<String, Object>();
  data08.put("image", R.drawable.study_other);
  data08.put("sort", "其他类");
  data.add(data08);
  return data;
 }*/
 @Override
 protected void onListItemClick(ListView l,View v,int position,long id){
  super.onListItemClick(l, v, position, id);
  switch (position){
  case 0:
   Intent intent=new Intent(study_Activity.this,Study_AnimalActivity.class);
   startActivity(intent);
   break;
   default:
    break;
  }
 }
}

3.Study_AnimalActivity.java

package com.example.lexueidiom;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

import com.example.lexueidiom.dao.AnimalDao;
import com.example.lexueidiom.entity.Animal;

import android.os.Bundle;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class Study_AnimalActivity extends ListActivity {
    private List<Map<String,Object>> data;
    private List<Animal> animals;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.list_animal);
  getData();
  SimpleAdapter simpleAdapter=new SimpleAdapter(this, data,
    R.layout.animal_item,new String[]{"idiom"},
    new int[]{R.id.idiom}){
  public View getView(final int position, View convertView,
    ViewGroup parent) {
   View view = super.getView(position, convertView, parent);

   final ImageButton imageButton = (ImageButton) view
     .findViewById(R.id.save);
   imageButton.setFocusable(false);// 这两句是关键
   imageButton.setFocusableInTouchMode(false);// 这两句是关键
   imageButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
     Toast.makeText(Study_AnimalActivity.this, "已收藏!",
       Toast.LENGTH_LONG).show();
    }
   });
   return view;
  }
 };
  setListAdapter(simpleAdapter);
 }

 protected void onListItemClick(ListView l, View v, int position, long id) {
  super.onListItemClick(l, v, position, id);
  AnimalDao animalDao=new AnimalDao(this);
  List<Animal> animals=animalDao.getAllAnimals();
  Animal animal= animals.get(position);
  String result = animal.getIdiom() + "\n" + animal.getSpell() + "\n【解释】: "
    + animal.getExplain() + "\n【近义词】: " +animal.getHomoionym()
    + "\n【反义词】: " + animal.getAntonym() + "\n【来源】: "
    +animal.getDerivation() + "\n【示例】: " + animal.getExamples()
    + "\n【英语翻译】: " + animal.getEnglish();
  showDialog(result);

 }

 private void showDialog(String result) {
  AlertDialog.Builder builder = new AlertDialog.Builder(this);
  LayoutInflater layoutInflater = LayoutInflater.from(this);
  View view = layoutInflater.inflate(R.layout.showview, null);
  builder.setView(view);
  TextView textView = (TextView) view.findViewById(R.id.show_idiom);
  textView.setText(result);
  builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

   @Override
   public void onClick(DialogInterface dialog, int which) {
    dialog.dismiss();
   }
  });
  builder.create().show();
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.study__animal, menu);
  return true;
 }
 private void getData(){
  data=new ArrayList<Map<String,Object>>();
  AnimalDao animalDao=new AnimalDao(this);
  List<Animal> animals=animalDao.getAllAnimals();
  for(Animal animal:animals){
   Map<String, Object> map=new HashMap<String,Object>();
   map.put("idiom",animal.getIdiom());
   data.add(map);
  }
 }
}

4.welcome_Activity.java

package com.example.lexueidiom;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;

public class welcome_Activity extends Activity {

 private ImageView welcomeImage;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_welcome);
  welcomeImage = (ImageView) findViewById(R.id.welcomeImage);
  AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
  alphaAnimation.setDuration(3000);
  welcomeImage.startAnimation(alphaAnimation);
  alphaAnimation.setAnimationListener(new AnimationListener() {

   @Override
   public void onAnimationStart(Animation animation) {
    // TODO Auto-generated method stub

   }

   @Override
   public void onAnimationRepeat(Animation animation) {
    // TODO Auto-generated method stub

   }

   @Override
   public void onAnimationEnd(Animation animation) {
    // TODO Auto-generated method stub
    Intent intent = new Intent();
    intent.setClass(welcome_Activity.this, MainActivity.class);
    startActivity(intent);
    finish();

   }
  });
  /*
   * Handler handler=new Handler(); handler.postDelayed(new Runnable() {
   *
   * @Override public void run() { Intent intent=new Intent();
   * intent.setClass(WelomeActivity.this, MainActivity.class);
   * startActivity(intent); finish(); } }, 3000);
   */

 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.welcome, menu);
  return true;
 }

}

 5.AnimalDao.java

package com.example.lexueidiom.dao;

import java.util.ArrayList;
import java.util.List;

import com.example.lexueidiom.db.DBHelper;
import com.example.lexueidiom.entity.Animal;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class AnimalDao {


 private DBHelper dbHelper;

 public AnimalDao(Context context) {
  dbHelper = new DBHelper(context);
 }

 public List<Animal> getAllAnimals() {
  List<Animal> animals=new ArrayList<Animal>();
  SQLiteDatabase sqLiteDatabase = dbHelper.openDatabase();
  Cursor cursor = sqLiteDatabase.rawQuery("select * from animal", null);
  while (cursor.moveToNext()) {
   int id = cursor.getInt(cursor.getColumnIndex("an_id"));
   String idiom = cursor.getString(cursor.getColumnIndex("an_idiom"));
   String spell = cursor.getString(cursor.getColumnIndex("an_spell"));
   String explain= cursor.getString(cursor.getColumnIndex("an_explain"));
   String antonym= cursor.getString(cursor.getColumnIndex("an_antonym"));
   String homoionym= cursor.getString(cursor.getColumnIndex("an_homoionym"));
   String derivation= cursor.getString(cursor.getColumnIndex("an_derivation"));
   String examples= cursor.getString(cursor.getColumnIndex("an_examples"));
   String english= cursor.getString(cursor.getColumnIndex("an_english"));
   Animal animal=new Animal(idiom, spell, explain, antonym, homoionym, derivation, examples, english);
   animal.setId(id);
   animals.add(animal);
  }
  cursor.close();
  sqLiteDatabase.close();
  return animals;
 }

}

6.DBHelper.java

package com.example.lexueidiom.db;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.example.lexueidiom.R;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;

public class DBHelper {
 private final int BUFFER_SIZE = 400000;
 public static final String DB_NAME = "idiom.db"; // 保存的数据库文件名
 public static final String PACKAGE_NAME = "com.example.lexueidiom";// 应用的包名
 public static final String DB_PATH = "/data"
   + Environment.getDataDirectory().getAbsolutePath() +"/"
   + PACKAGE_NAME+ "/databases"; // 在手机里存放数据库的位置
 //sdcard的路径
 //public static final String DB_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()+"/idiom";

 private Context context;

 public DBHelper(Context context) {
  this.context = context;
 }


 public SQLiteDatabase openDatabase() {
  try {
   File myDataPath = new File(DB_PATH);
   if (!myDataPath.exists())
   {  
    myDataPath.mkdirs();// 如果没有这个目录则创建
   }
   String dbfile=myDataPath+"/"+DB_NAME;
   if (!(new File(dbfile).exists())) {// 判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库
    InputStream is = context.getResources().openRawResource(
      R.raw.idiom); // 欲导入的数据库
    FileOutputStream fos = new FileOutputStream(dbfile);
    byte[] buffer = new byte[BUFFER_SIZE];
    int count = 0;
    while ((count = is.read(buffer)) > 0) {
     fos.write(buffer, 0, count);
    }
    fos.close();
    is.close();
   }
   SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,
     null);
   return db;
  } catch (FileNotFoundException e) {
   Log.e("Database", "File not found");
   e.printStackTrace();
  } catch (IOException e) {
   Log.e("Database", "IO exception");
   e.printStackTrace();
  }
  return null;
 }
}

 7.Animal.java

 

package com.example.lexueidiom.entity;
public class Animal {
 private int id;
    private String idiom;
    private String spell;
    private String explain;
    private String antonym;
    private String homoionym;
    private String derivation;
    private String examples;
    private String english;
 public Animal(String idiom, String spell, String explain, String antonym,
   String homoionym, String derivation, String examples, String english) {
  super();
  this.idiom = idiom;
  this.spell = spell;
  this.explain = explain;
  this.antonym = antonym;
  this.homoionym = homoionym;
  this.derivation = derivation;
  this.examples = examples;
  this.english = english;
 }
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getIdiom() {
  return idiom;
 }
 public void setIdiom(String idiom) {
  this.idiom = idiom;
 }
 public String getSpell() {
  return spell;
 }
 public void setSpell(String spell) {
  this.spell = spell;
 }
 public String getExplain() {
  return explain;
 }
 public void setExplain(String explain) {
  this.explain = explain;
 }
 public String getAntonym() {
  return antonym;
 }
 public void setAntonym(String antonym) {
  this.antonym = antonym;
 }
 public String getHomoionym() {
  return homoionym;
 }
 public void setHomoionym(String homoionym) {
  this.homoionym = homoionym;
 }
 public String getDerivation() {
  return derivation;
 }
 public void setDerivation(String derivation) {
  this.derivation = derivation;
 }
 public String getExamples() {
  return examples;
 }
 public void setExamples(String examples) {
  this.examples = examples;
 }
 public String getEnglish() {
  return english;
 }
 public void setEnglish(String english) {
  this.english = english;
 }
 @Override
 public String toString() {
  return "Animal [id=" + id + ", idiom=" + idiom + ", spell=" + spell
    + ", explain=" + explain + ", antonym=" + antonym
    + ", homoionym=" + homoionym + ", derivation=" + derivation
    + ", examples=" + examples + ", english=" + english + "]";
 }
 
}

8.DBHelperTest.java

package com.example.lexueidiom.Test;


import com.example.lexueidiom.dao.AnimalDao;
import com.example.lexueidiom.db.DBHelper;

import android.test.AndroidTestCase;

public class DBHelperTest extends AndroidTestCase{
    public void testDBCreate(){
     DBHelper dbHelper=new DBHelper(getContext());
     dbHelper.openDatabase();
    }
    public void testGetAnimals(){
     AnimalDao animalDao=new AnimalDao(getContext());
     System.out.println(animalDao.getAllAnimals());
   }
9.布局

<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: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=".MainActivity" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <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="fill_parent"
                android:layout_height="match_parent">

                <include layout="@layout/activity_study"/>
                <include layout="@layout/activity_search"/>
                <include layout="@layout/activity_game"/>
                <include layout="@layout/activity_save"/>
                <include layout="@layout/activity_help"/>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
   
</RelativeLayout>

10.进入页面

<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=".WelomeActivity" >

    <ImageView
        android:id="@+id/welcomeImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/welcome" />

    <ProgressBar
        android:id="@+id/progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="93dp" />

    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="@string/competition"
        android:textSize="30sp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>


 


 

 

 

 


 

 

0 0
原创粉丝点击