码迷,mamicode.com
首页 > 移动开发 > 详细

Android能发音的生词本

时间:2016-03-11 06:41:14      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:android

---前台界面部分


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/beijing"
    android:orientation="vertical"
    tools:context=".MainActivity" >

   
   
     <EditText
        android:id="@+id/word"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/_hint_word"
        android:layout_marginTop="25sp"
        android:ems="10" >

     
    </EditText>
  <EditText
        android:id="@+id/jieshi"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/_jieshi" 
        android:layout_marginTop="25sp"
        android:ems="10" >
   </EditText>
 
    <Button
        android:id="@+id/insert"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25sp"
        android:text="@string/_insert"
        android:textSize="20sp"
        android:textColor="@android:color/holo_blue_bright"
        android:background="@android:color/holo_green_light"
       />  
      
    <EditText
        android:id="@+id/sh"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/_sh"  
        android:layout_marginTop="5sp"
        android:ems="10" >
   </EditText>
     <Button
        android:id="@+id/search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25sp"
        android:text="@string/_search" 
        android:textSize="20sp"
        android:textColor="@android:color/holo_green_light"
        android:background="@android:color/holo_blue_bright"
       />    
       <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/_yw"
        android:gravity="center"
        android:textColor="@android:color/holo_orange_dark"
        android:textSize="25sp"
       />
       <TextView
        android:id="@+id/yw"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="25sp"
        android:gravity="center"
        android:textColor="@android:color/holo_red_light"
        android:layout_marginTop="5sp"
       />
   <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/_dcys"
        android:gravity="center"
        android:textColor="@android:color/holo_purple"
        android:textSize="25sp"
       />
     <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5sp"
        android:gravity="center"
        android:textColor="@android:color/holo_red_light"
        android:textSize="25sp"
       />
</LinearLayout>


----- MyDBHelper

package com.example.note;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDBHelper extends SQLiteOpenHelper {

 public MyDBHelper(Context context, String name, CursorFactory factory,
   int version) {
  super(context, name, factory, version);
 }

 @Override
 public void onCreate(SQLiteDatabase db) {
     db.execSQL("create table word_scb(_id integer primary key autoincrement," +
       "word,detail)");
 }

 @Override
 public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {

 }

}

----MainActivity

package com.example.note;

import java.util.Locale;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
private TextView dcys;
private TextView ywdc;
private MyDBHelper myDBHelper;
private Button insert;//插入
private Button search;//查找
private EditText et1;//单词
private EditText et2;//解释
private EditText cz;
private TextToSpeech tts;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  myDBHelper=new MyDBHelper(MainActivity.this,"danci.db",null,3);
  SQLiteDatabase db=myDBHelper.getWritableDatabase();
  //db.execSQL("insert into word_scb (word,detail) values(‘vds‘,‘hh‘)");
  et1=(EditText)findViewById(R.id.word);
  et2=(EditText)findViewById(R.id.jieshi);
  insert=(Button)findViewById(R.id.insert);
  search=(Button)findViewById(R.id.search);
  dcys=(TextView)findViewById(R.id.tv);
  ywdc=(TextView)findViewById(R.id.yw);
  cz=(EditText)findViewById(R.id.sh);
  //单词的朗读
  tts=new TextToSpeech(MainActivity.this,new OnInitListener() {
   
   @Override
   public void onInit(int status) {
    if(tts.SUCCESS==status){
     int result=tts.setLanguage(Locale.US);
     //int lan=tts.setLanguage(Locale.CHINESE);
     if(result!=TextToSpeech.LANG_COUNTRY_AVAILABLE&&result!=TextToSpeech.LANG_AVAILABLE){
      Toast.makeText(MainActivity.this,"暂时不支持该语种朗读 !",Toast.LENGTH_LONG).show();
     }
    }
    
   }
  });
  //添加单词与解释
  insert.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    SQLiteDatabase db=myDBHelper.getWritableDatabase();
    ContentValues cValues=new ContentValues();
    String str1=et1.getText().toString().trim();
    String str2=et2.getText().toString().trim();
    if(str1.length()==0|str2.length()==0){
      Toast.makeText(MainActivity.this,"请添加生词",Toast.LENGTH_SHORT).show();
    }else{
    cValues.put("word",str1);
    cValues.put("detail",str2);
    db.insert("word_scb",null, cValues);
    Toast.makeText(MainActivity.this,"添加生词成功",Toast.LENGTH_LONG).show();
    }
    cValues.clear();
    db.close();
    
    
   }
  });
  
  //查找单词与解释
  search.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View arg0) {
    String key=cz.getText().toString().trim();
    
    SQLiteDatabase  db=myDBHelper.getReadableDatabase();
    tts.speak(cz.getText().toString(), TextToSpeech.QUEUE_ADD, null);
    if(key.length()==0){
     Toast.makeText(MainActivity.this,"请输入单词或者该单词意思",Toast.LENGTH_LONG).show();
    }else{
    Cursor cursor=db.rawQuery("select * from word_scb where word like ‘"+key+"‘ or detail like ‘"+key+"‘",null);
    
    while(cursor.moveToNext()){
     
     String ws=cursor.getString(cursor.getColumnIndex("word"));
     String del=cursor.getString(cursor.getColumnIndex("detail"));
     
     dcys.setText(del);
     
     ywdc.setText(ws);
     
    
     
    }
    
    db.close();
    }
   }
  });
 }
 protected void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
  if(tts !=null){
   tts.stop();
  }
 }

}

--非常简单在这里就不一 一解释了,谢谢大家关注

本文出自 “随笔” 博客,请务必保留此出处http://lishouxiang.blog.51cto.com/8123559/1749707

Android能发音的生词本

标签:android

原文地址:http://lishouxiang.blog.51cto.com/8123559/1749707

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!