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

Android有道词典查询功能

时间:2015-07-04 12:28:16      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:

有道词典

任务要求:完成查词等功能

技术分享

 

因为需要申请API key,这里直接给出地址供使用:http://fanyi.youdao.com/openapi?path=data-mode

1、activity_main.xml基本格局(不做任何说明)

代码如下:

技术分享
 1 <RelativeLayout   
 2     xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:background="@drawable/wyf"
 7     android:paddingBottom="@dimen/activity_vertical_margin"
 8     android:paddingLeft="@dimen/activity_horizontal_margin"
 9     android:paddingRight="@dimen/activity_horizontal_margin"
10     android:paddingTop="@dimen/activity_vertical_margin"
11     tools:context=".MainActivity" >
12 
13     <EditText
14         android:id="@+id/etWord"
15         android:layout_width="wrap_content"
16         android:layout_height="wrap_content"
17         android:layout_alignParentLeft="true"
18         android:layout_alignParentTop="true"
19         android:layout_marginTop="31dp"
20         android:background="@android:drawable/edit_text"
21         android:ems="10"
22         android:hint="@string/searchHint"
23         android:singleLine="true"
24         android:textColor="#552006"
25         android:textColorHint="#782f10" >
26 
27         <requestFocus />
28     </EditText>
29 
30     <Button
31         android:id="@+id/btnSearch"
32         android:layout_width="wrap_content"
33         android:layout_height="wrap_content"
34         android:layout_alignBaseline="@+id/etWord"
35         android:layout_alignBottom="@+id/etWord"
36         android:layout_marginLeft="16dp"
37         android:layout_toRightOf="@+id/etWord"
38         android:background="@drawable/ibsearchword"
39         android:onClick="searchWord"
40         android:text="@string/serachWord" />
41 
42     <TextView
43         android:id="@+id/tvSearchResult"
44         android:layout_width="match_parent"
45         android:layout_height="match_parent"
46         android:layout_alignLeft="@+id/etWord"
47         android:layout_below="@+id/etWord"
48         android:layout_marginTop="22dp"
49         android:textSize="25sp"
50         android:background="@drawable/ff"
51         android:textAppearance="?android:attr/textAppearanceMedium" />
52 
53 </RelativeLayout>
activity_main.xml

注意:这里使用WebView、TextView是一样的,布局格式可随意更换。

2、MainActivity

(1)代码如下:

技术分享
 1 public class MainActivity extends Activity {
 2     private WordDao wordDao;
 3     private EditText etWord;
 4     private TextView tvResult;
 5 
 6     @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8         super.onCreate(savedInstanceState);
 9         setContentView(R.layout.activity_main);
10 
11         initViews();
12     }
13 
14     private void initViews() {
15         etWord = (EditText) findViewById(R.id.etWord);
16         tvResult = (TextView) findViewById(R.id.tvSearchResult);
17     }
18 
19     @Override
20     public boolean onCreateOptionsMenu(Menu menu) {
21         getMenuInflater().inflate(R.menu.main, menu);
22         return true;
23     }
24 
25     public void searchWord(View view) {
26         String english = etWord.getText().toString();
27         wordDao = new WordDao(this);
28         if (TextUtils.isEmpty(english)) {
29             tvResult.setText("请输入您想查询的单词");
30         } else {
31             String chinese = wordDao.getChinese(english);
32             tvResult.setText(chinese);
33         }
34     }
35 
36 }
主要代码

注意:自定义的java类较多不在一一列出,这里介绍其他MainActivity的写法。

(2)代码如下:

技术分享
 1 public class MainActivity extends Activity {
 2     private EditText etWord;
 3     private WebView tvResult;
 4 
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.activity_main);
 9 
10         initViews();
11     }
12 
13     private void initViews() {
14         etWord = (EditText) findViewById(R.id.etWord);
15         tvResult = (WebView) findViewById(R.id.tvSearchResult);
16         tvResult.setWebViewClient(new WebViewClient() {
17             @Override
18             public boolean shouldOverrideUrlLoading(WebView view, String url) {
19                 view.loadUrl(url);
20                 return true;
21             }
22         });
23     }
24 
25     @Override
26     public boolean onCreateOptionsMenu(Menu menu) {
27         getMenuInflater().inflate(R.menu.main, menu);
28         return true;
29     }
30 
31     public void searchWord(View view) {
32         String word = etWord.getText().toString();
33         if (TextUtils.isEmpty(word)) {
34             Toast.makeText(this, "内容不为空", Toast.LENGTH_LONG).show();
35         } else {
36 
37             final String strUrl = "http://dict.youdao.com/m/search?keyfrom=dict.mindex&vendor=&q="
38                     + word;
39             wvResult.loadUrl(strUrl);
40         }
41     }
42 }
主要代码

(3)代码如下:

技术分享
 1 public class MainActivity extends Activity {
 2  private Button btnSearch;
 3  private Button btnClear;
 4  private EditText editText;
 5  private WebView reswebView;
 6  private void SetView() {
 7   btnSearch = (Button) findViewById(R.id.btnsearch);
 8   btnClear = (Button) findViewById(R.id.btnclear);
 9   editText = (EditText) findViewById(R.id.editText);
10   reswebView = (WebView) findViewById(R.id.reswebView);
11   btnSearch.setOnClickListener(new OnClickListener() {
12    @Override
13    public void onClick(View v) {
14     String strUri = editText.getText().toString();
15     strUri = strUri.trim();
16     if (strUri.length() == 0) {
17      Toast.makeText(getApplicationContext(), "请输入查询字符", 1).show();
18     } else {
19      String strURL = "http://dict.youdao.com/m/search?keyfrom=dict.mindex&q=" + strUri;
20      reswebView.loadUrl(strURL);
21     }
22    }
23   });
24   btnClear.setOnClickListener(new OnClickListener() {
25    @Override
26    public void onClick(View v) {
27     editText.setText("");
28    }
29   });
30  }
31  @Override
32  protected void onCreate(Bundle savedInstanceState) {
33   super.onCreate(savedInstanceState);
34   setContentView(R.layout.activity_main);
35   SetView();
36  }
37 }
主要代码

注意:最后不要忘了添加访问权限

代码如下:

1 <uses-permission android:name="android.permission.INTERNET" />

程序完成!

如图:

技术分享

代码仅供参考!!

Android有道词典查询功能

标签:

原文地址:http://www.cnblogs.com/j0820/p/4620411.html

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