有道词典运行效果图如下
一、布局文件
1、layout_main.xml
<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"
android:background="@drawable/c"
tools:context=".MainActivity" >
<EditText
android:id="@+id/etWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="27dp"
android:ems="10"
android:background="@android:drawable/edit_text"
android:singleLine="true"
android:textColor="#552006"
android:textColorHint="#782f10" >
<requestFocus />
</EditText>
<WebView
android:id="@+id/wvSearch"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignLeft="@+id/etWord"
android:layout_below="@+id/etWord"
android:layout_marginTop="22dp"
android:background="@drawable/c"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="25sp" />
<Button
android:id="@+id/btnSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/wvSearch"
android:layout_alignParentRight="true"
android:onClick="searchWord"
android:text="查询"/>
</RelativeLayout>
2、AndroidManifest.xml
其中最重要的一句
没有这句会报异常
下面是主要的java代码
package com.example.youdao;
import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText etWord;
private WebView wvSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
// TODO Auto-generated method stub
etWord=(EditText) findViewById(R.id.etWord);
wvSearch=(WebView) findViewById(R.id.wvSearch);
wvSearch.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view,String url){
view.loadUrl(url);
return true;
}
});
}
@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;
}
public void searchWord(View view){
String word=etWord.getText().toString();
if(TextUtils.isEmpty(word)){
Toast.makeText(this, "查询内容不能为空", Toast.LENGTH_LONG).show();
}else{
final String strUrl="http://dict.youdao.com/m/search?keyfrom=dict.mindex&vendor=&q="
+word;
wvSearch.loadUrl(strUrl);
}
}
}
<p>版权声明:本文为博主原创文章,未经博主允许不得转载。</p>