实现有道词典功能的界面

一:不管你输入什么单词,它都会调用网页版的有道词典,并且在WebView中把该单词的读音,解释给显示出来。

 

代码如下:

复制代码
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context=".MainActivity" >
10 
11     <EditText
12         android:id="@+id/edtWord"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:layout_alignBaseline="@+id/btn"
16         android:layout_alignBottom="@+id/btn"
17         android:layout_alignParentLeft="true"
18         android:ems="10" >
19 
20         <requestFocus />
21     </EditText>
22     <Button
23         android:id="@+id/btnFind"
24         android:layout_width="wrap_content"
25         android:layout_height="wrap_content"
26         android:layout_alignParentRight="true"
27         android:layout_alignParentTop="true"
28         android:onClick="findWord"
29         android:layout_marginTop="58dp"
30         android:text="@string/btnFind" />
31     
32    <WebView android:id="@+id/webResult"
33      android:layout_width="match_parent"
34      android:layout_height="match_parent"
35      android:layout_alignLeft="@+id/edt"
36      android:layout_below="@+id/edt"
37      android:layout_marginTop="22dp"   
38      android:textSize="25sp"
39      >
40      
41  </WebView>
42      
43 
44 </RelativeLayout>
复制代码

MainActivity的代码:

复制代码
 1 public class MainActivity extends Activity {
 2     Button btn;//查找按钮
 3     WebView wv;//显示控件
 4     EditText tt;//输入文本
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.activity_main);
 9         findView();//获取控件
10     }
11     /**
12      * 获取控件的id
13      */
14     @SuppressWarnings("unused")
15     private void findView() {
16         btn=(Button) findViewById(R.id.btnFind);
17         wv=(WebView) findViewById(R.id.webResult);
18         tt=(EditText) findViewById(R.id.edtWord);
19     }
20     public void findWord(View view) {
21         String strText=(tt.getText().toString()).trim();//除去字符串开头和末尾的空格或其他字符
22         if (strText.length()==0) {//判断查找的单词是否为空
23             Toast.makeText(MainActivity.this, "查询内容不能为空!", Toast.LENGTH_LONG).show();
24         }
25         else{
26             String strURL="http://dict.youdao.com/m/search?keyfrom=dict.mindex&q="+strText;//加载路径
27             wv.loadUrl(strURL);//加载到WebView控件上显示
28             wv.setWebViewClient(new WebViewClient() {//禁止调用系统浏览器
29                 public boolean shouldOverrideUrlLoading(WebView view, String url){
30                     view.loadUrl(url);
31                     return false;
32                 }
33             });
34             
35         }
36     }
37 }
复制代码


注意:可能会出现空指针现象,在定义Button或其他空间是,要注意给它赋值,也就是findViewById,并强制转换成Button类型货其他类型。。

 

posted @ 2015-07-01 22:55  孙伟强  阅读(314)  评论(0编辑  收藏  举报