├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── example │ │ └── george │ │ └── dictionary │ │ ├── AudioService.java │ │ ├── DicPage.java │ │ ├── MainActivity.java │ │ └── TranPage.java │ └── res │ ├── layout │ ├── activity_bottom.xml │ ├── activity_main.xml │ ├── dictionary.xml │ └── translation.xml │ └── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml └── img_folder ├── 065F42DD4A03E3A54EC8F21197E823A4.jpg ├── 1F6BECD33D0597F2AF65D860893D9B33.jpg └── A1E5C076BC61B7727CE1DE454BA1824E.jpg /README.md: -------------------------------------------------------------------------------- 1 | # Dictionary 2 | Simple Chinese-English translation tool& Chinese Dictionary Tools 3 | 1. Chinese and English two-way translation 4 | 5 | ![Search Chinese](https://github.com/GeorgeCh2/Dictionary/blob/master/img_folder/065F42DD4A03E3A54EC8F21197E823A4.jpg) 6 | 7 | 1.1 Pronounceable queries in English 8 | 9 | ![Inquiry English](https://github.com/GeorgeCh2/Dictionary/blob/master/img_folder/1F6BECD33D0597F2AF65D860893D9B33.jpg) 10 | 11 | 2. Chinese Dictionary 12 | 13 | ![Inquiry Chinese](https://github.com/GeorgeCh2/Dictionary/blob/master/img_folder/A1E5C076BC61B7727CE1DE454BA1824E.jpg) 14 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.2" 6 | defaultConfig { 7 | applicationId "com.example.george.dictionary" 8 | minSdkVersion 15 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:25.3.1' 28 | compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7' 29 | testCompile 'junit:junit:4.12' 30 | compile 'com.kymjs.rxvolley:rxvolley:1.1.4' 31 | } 32 | 33 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in E:\SDK/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/george/dictionary/AudioService.java: -------------------------------------------------------------------------------- 1 | package com.example.george.dictionary; 2 | 3 | import android.app.Service; 4 | import android.content.Intent; 5 | import android.media.MediaPlayer; 6 | import android.net.Uri; 7 | import android.os.IBinder; 8 | import android.support.annotation.Nullable; 9 | 10 | /** 11 | * Created by George on 2017/6/3. 12 | */ 13 | 14 | public class AudioService extends Service { 15 | private MediaPlayer mediaPlayer; 16 | private String query; 17 | 18 | @Override 19 | public void onCreate() { 20 | super.onCreate(); 21 | } 22 | 23 | @Override 24 | public void onStart(Intent intent, int startId) { 25 | if (query != null && !query.equals(intent.getStringExtra("query")) && mediaPlayer != null) { 26 | mediaPlayer.start(); 27 | } else { 28 | String query = intent.getStringExtra("query"); 29 | String contain = "http://dict.youdao.com/dictvoice?audio=" + query; 30 | Uri location = Uri.parse(contain); 31 | 32 | mediaPlayer = MediaPlayer.create(this, location); 33 | mediaPlayer.prepareAsync(); 34 | mediaPlayer.start(); 35 | 36 | 37 | // 播放音乐时发生错误的事件处理 38 | mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() { 39 | public boolean onError(MediaPlayer mp, int what, int extra) { 40 | // 释放资源 41 | try { 42 | mp.release(); 43 | } catch (Exception e) { 44 | e.printStackTrace(); 45 | } 46 | return false; 47 | } 48 | }); 49 | } 50 | } 51 | 52 | @Override 53 | public void onDestroy() { 54 | mediaPlayer.stop(); 55 | mediaPlayer.release(); 56 | super.onDestroy(); 57 | } 58 | 59 | @Nullable 60 | @Override 61 | public IBinder onBind(Intent intent) { 62 | return null; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/george/dictionary/DicPage.java: -------------------------------------------------------------------------------- 1 | package com.example.george.dictionary; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.Button; 10 | import android.widget.EditText; 11 | import android.widget.LinearLayout; 12 | import android.widget.TextView; 13 | 14 | import com.kymjs.rxvolley.RxVolley; 15 | import com.kymjs.rxvolley.client.HttpCallback; 16 | import com.kymjs.rxvolley.toolbox.Loger; 17 | 18 | import org.json.JSONArray; 19 | import org.json.JSONException; 20 | import org.json.JSONObject; 21 | 22 | import java.io.UnsupportedEncodingException; 23 | import java.net.URLEncoder; 24 | 25 | /** 26 | * Created by George on 2017/6/3. 27 | */ 28 | 29 | public class DicPage extends Fragment { 30 | //输入框 31 | private EditText dic_input; 32 | //搜索按钮 33 | private Button dic_search; 34 | //基本信息布局 35 | private LinearLayout dic_information; 36 | //要查询的内容 37 | private TextView dic_basic; 38 | //发音按钮 39 | private Button dic_pron; 40 | //音标 41 | private TextView dic_phonetic; 42 | //释义 43 | private TextView dic_interpre; 44 | @Nullable 45 | @Override 46 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 47 | View view = inflater.inflate(R.layout.dictionary, null); 48 | dic_input = (EditText) view.findViewById(R.id.dic_input); 49 | dic_search = (Button) view.findViewById(R.id.dic_search); 50 | dic_information = (LinearLayout) view.findViewById(R.id.dic_information); 51 | dic_basic = (TextView) view.findViewById(R.id.dic_basic); 52 | dic_pron = (Button) view.findViewById(R.id.dic_pron); 53 | dic_phonetic = (TextView) view.findViewById(R.id.dic_phonetic); 54 | dic_interpre = (TextView) view.findViewById(R.id.dic_interpre); 55 | 56 | //查询之前基本信息不可见 57 | dic_information.setVisibility(View.GONE); 58 | return view; 59 | } 60 | 61 | @Override 62 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { 63 | super.onActivityCreated(savedInstanceState); 64 | 65 | dic_search.setOnClickListener(new View.OnClickListener() { 66 | @Override 67 | public void onClick(View v) { 68 | String word = dic_input.getText().toString().trim(); 69 | 70 | try { 71 | word = URLEncoder.encode(word, "utf-8"); 72 | } catch (UnsupportedEncodingException e) { 73 | e.printStackTrace(); 74 | } 75 | 76 | if (null != word && !"".equals(word)) { 77 | String contans = "http://v.juhe.cn/xhzd/query?key=2dfdd9bc5b46fc8b6aa7bdfd682dd80d&word=" + word; 78 | 79 | //执行网络请求 80 | RxVolley.get(contans, new HttpCallback() { 81 | @Override 82 | public void onSuccess(String t) { 83 | Loger.debug("请求的数据:" + t); 84 | parseJson(t); 85 | } 86 | 87 | private void parseJson(String json) { 88 | try { 89 | JSONObject jsonObject = new JSONObject(json); 90 | String basic = null; 91 | String phonetic = null; 92 | String interpre = null; 93 | 94 | if (jsonObject.getInt("error_code") == 0) { 95 | if (jsonObject.has("result")) { 96 | JSONObject resultObject = jsonObject.getJSONObject("result"); 97 | 98 | if(resultObject.has("zi")) { 99 | basic = resultObject.getString("zi"); 100 | } 101 | 102 | if(resultObject.has("pinyin")) { 103 | phonetic = resultObject.getString("pinyin"); 104 | } 105 | 106 | if (resultObject.has("jijie")) { 107 | JSONArray jijieArr = resultObject.getJSONArray("jijie"); 108 | interpre = jijieArr.getString(2); 109 | for (int i = 3; i < jijieArr.length()-4; i ++) { 110 | interpre += "\n" + jijieArr.getString(i); 111 | } 112 | } 113 | } 114 | } 115 | 116 | dic_information.setVisibility(View.VISIBLE); 117 | //清空信息 118 | dic_basic.setText(""); 119 | dic_phonetic.setText(""); 120 | dic_interpre.setText(""); 121 | 122 | dic_basic.setText(basic); 123 | dic_phonetic.setText(phonetic); 124 | dic_interpre.setText(interpre); 125 | } catch (JSONException e) { 126 | e.printStackTrace(); 127 | } 128 | } 129 | }); 130 | } 131 | } 132 | }); 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/george/dictionary/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.george.dictionary; 2 | 3 | import android.os.Build; 4 | import android.support.v4.app.Fragment; 5 | import android.support.v4.app.FragmentActivity; 6 | import android.support.v4.app.FragmentPagerAdapter; 7 | import android.support.v4.view.ViewPager; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.os.Bundle; 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | import android.view.Window; 13 | import android.view.WindowManager; 14 | import android.widget.Button; 15 | 16 | import java.util.ArrayList; 17 | import java.util.List; 18 | 19 | public class MainActivity extends FragmentActivity implements View.OnClickListener { 20 | //底部两个按钮 21 | private Button translation; 22 | private Button dictionary; 23 | 24 | //中间内容区域 25 | private ViewPager viewPager; 26 | 27 | //页面集合 28 | private List fragments; 29 | 30 | //两个Fragment 31 | private TranPage tranPage; 32 | private DicPage dicPage; 33 | 34 | //选项卡总数 35 | private static final int TAB_COUNT = 2; 36 | 37 | //当前显示的选项卡位置 38 | private int current_index = 0; 39 | 40 | @Override 41 | protected void onCreate(Bundle savedInstanceState) { 42 | super.onCreate(savedInstanceState); 43 | setContentView(R.layout.activity_main); 44 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 45 | // 设置状态栏透明 46 | this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 47 | 48 | } 49 | //初始化控件 50 | initView(); 51 | 52 | //初始化底部按钮事件 53 | initEvent(); 54 | 55 | } 56 | 57 | private void initEvent() { 58 | //为按钮注册监听事件 59 | translation.setOnClickListener(this); 60 | dictionary.setOnClickListener(this); 61 | } 62 | 63 | private void initView() { 64 | //底部按钮 65 | this.translation = (Button) findViewById(R.id.translation); 66 | this.dictionary = (Button) findViewById(R.id.dictionary); 67 | 68 | //中间内容区域 69 | this.viewPager = (ViewPager) findViewById(R.id.content); 70 | 71 | fragments = new ArrayList(); 72 | tranPage = new TranPage(); 73 | dicPage = new DicPage(); 74 | 75 | fragments.add(tranPage); 76 | fragments.add(dicPage); 77 | 78 | viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { 79 | @Override 80 | public Fragment getItem(int position) { 81 | return fragments.get(position); 82 | } 83 | 84 | @Override 85 | public int getCount() { 86 | return fragments.size(); 87 | } 88 | }); 89 | } 90 | 91 | @Override 92 | public void onClick(View v) { 93 | switch (v.getId()) { 94 | case R.id.translation: 95 | translation.setBackgroundColor(0xffD9D9D9); 96 | dictionary.setBackgroundColor(0xFFFFFFFF); 97 | viewPager.setCurrentItem(0); 98 | break; 99 | 100 | case R.id.dictionary: 101 | dictionary.setBackgroundColor(0xffD9D9D9); 102 | translation.setBackgroundColor(0xFFFFFFFF); 103 | viewPager.setCurrentItem(1); 104 | break; 105 | 106 | default: 107 | break; 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/george/dictionary/TranPage.java: -------------------------------------------------------------------------------- 1 | package com.example.george.dictionary; 2 | 3 | import android.content.Intent; 4 | import android.media.MediaPlayer; 5 | import android.net.Uri; 6 | import android.os.Bundle; 7 | import android.support.annotation.Nullable; 8 | import android.support.v4.app.Fragment; 9 | import android.view.LayoutInflater; 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | import android.widget.Button; 13 | import android.widget.EditText; 14 | import android.widget.LinearLayout; 15 | import android.widget.TextView; 16 | 17 | import com.kymjs.rxvolley.RxVolley; 18 | import com.kymjs.rxvolley.client.HttpCallback; 19 | import com.kymjs.rxvolley.toolbox.Loger; 20 | 21 | import org.json.JSONArray; 22 | import org.json.JSONException; 23 | import org.json.JSONObject; 24 | 25 | import java.io.IOException; 26 | import java.io.UnsupportedEncodingException; 27 | import java.net.URLEncoder; 28 | 29 | /** 30 | * Created by George on 2017/6/3. 31 | */ 32 | 33 | public class TranPage extends Fragment implements View.OnClickListener { 34 | //输入框 35 | private EditText tran_input; 36 | //搜索按钮 37 | private Button tran_search; 38 | //基本信息布局 39 | private LinearLayout tran_information; 40 | //要查询的内容 41 | private TextView tran_basic; 42 | //发音按钮 43 | private Button tran_pron; 44 | //音标 45 | private TextView tran_phonetic; 46 | //释义 47 | private TextView tran_interpre; 48 | 49 | private MediaPlayer mediaPlayer; 50 | 51 | @Nullable 52 | @Override 53 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 54 | View view = inflater.inflate(R.layout.translation, null); 55 | tran_input = (EditText) view.findViewById(R.id.tran_input); 56 | tran_search = (Button) view.findViewById(R.id.tran_search); 57 | tran_information = (LinearLayout) view.findViewById(R.id.tran_information); 58 | tran_basic = (TextView) view.findViewById(R.id.tran_basic); 59 | tran_pron = (Button) view.findViewById(R.id.tran_pron); 60 | tran_phonetic = (TextView) view.findViewById(R.id.tran_phonetic); 61 | tran_interpre = (TextView) view.findViewById(R.id.tran_interpre); 62 | 63 | //查询之前基本信息不可见 64 | tran_information.setVisibility(View.GONE); 65 | 66 | mediaPlayer = new MediaPlayer(); 67 | return view; 68 | } 69 | 70 | @Override 71 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { 72 | super.onActivityCreated(savedInstanceState); 73 | 74 | //查询按钮点击事件 75 | tran_search.setOnClickListener(this); 76 | 77 | //发音按钮点击事件 78 | tran_pron.setOnClickListener(this); 79 | } 80 | 81 | @Override 82 | public void onClick(View v) { 83 | switch (v.getId()) { 84 | case R.id.tran_search: 85 | search(); 86 | break; 87 | case R.id.tran_pron: 88 | try { 89 | player(); 90 | } catch (IOException e) { 91 | e.printStackTrace(); 92 | } 93 | break; 94 | 95 | default: 96 | break; 97 | } 98 | } 99 | 100 | private void player() throws IOException { 101 | String word = tran_input.getText().toString().trim(); 102 | final String contain = "http://dict.youdao.com/dictvoice?audio=" + word; 103 | 104 | mediaPlayer.setDataSource(getActivity(), Uri.parse(contain)); 105 | mediaPlayer.prepareAsync(); 106 | mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 107 | @Override 108 | public void onPrepared(MediaPlayer mediaPlayer) { 109 | mediaPlayer.start(); 110 | } 111 | }); 112 | mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 113 | @Override 114 | public void onCompletion(MediaPlayer mp) { 115 | mediaPlayer.reset(); 116 | } 117 | }); 118 | } 119 | 120 | private void search() { 121 | String word = tran_input.getText().toString().trim(); 122 | try { 123 | word = URLEncoder.encode(word, "utf-8"); 124 | } catch (UnsupportedEncodingException e) { 125 | e.printStackTrace(); 126 | } 127 | 128 | if (null != word && !"".equals(word)) { 129 | //拼接URL 130 | final String contants = "http://fanyi.youdao.com/openapi.do?keyfrom=Georgechou&key=1912076918&type=data&doctype=json&version=1.1&q=" + word; 131 | 132 | //执行网络请求 133 | RxVolley.get(contants, new HttpCallback() { 134 | @Override 135 | public void onSuccess(String t) { 136 | Loger.debug("请求的数据:" + t); 137 | parseJson(t); 138 | } 139 | }); 140 | } 141 | } 142 | 143 | //解析json数据 144 | private void parseJson(String json) { 145 | try { 146 | JSONObject jsonObject = new JSONObject(json); 147 | String basic = null; 148 | String phonetic = null; 149 | String interpre = null; 150 | 151 | if (jsonObject.getInt("errorCode") == 0) { 152 | basic = jsonObject.getString("query"); 153 | 154 | if (jsonObject.has("basic")) { 155 | JSONObject basicObject = jsonObject.getJSONObject("basic"); 156 | 157 | if (basicObject.has("phonetic")) { 158 | phonetic = basicObject.getString("phonetic"); 159 | } 160 | 161 | if (basicObject.has("explains")) { 162 | JSONArray explainArr = basicObject.getJSONArray("explains"); 163 | interpre = explainArr.getString(0); 164 | for (int i = 1; i < explainArr.length(); i++) { 165 | interpre += "\n" + explainArr.getString(i); 166 | } 167 | } 168 | } 169 | } 170 | //使基本信息布局可见 171 | tran_information.setVisibility(View.VISIBLE); 172 | //清空信息 173 | tran_basic.setText(""); 174 | tran_phonetic.setText(""); 175 | tran_interpre.setText(""); 176 | 177 | tran_basic.setText(basic); 178 | tran_phonetic.setText(phonetic); 179 | tran_interpre.setText(interpre); 180 | } catch (JSONException e) { 181 | e.printStackTrace(); 182 | } 183 | } 184 | } 185 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_bottom.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 |