标签:
1:建立activity_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"
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:background="@android:drawable/edit_text"
android:ems="10"
android:singleLine="true"
android:textColor="#552006"
android:textColorHint="#782f10" >
<requestFocus />
</EditText>
<WebView
android:id="@+id/wvSearchResult"
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:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="25sp" />
<Button
android:id="@+id/btnSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/etWord"
android:layout_alignBottom="@+id/etWord"
android:layout_toRightOf="@+id/etWord"
android:text="查询"
android:onClick="search" />
</RelativeLayout>
2:JAVA类:
package com.hnz.youdaodectionary;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText etWord;
private WebView wvSearchResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etWord = (EditText) findViewById(R.id.etWord);
wvSearchResult = (WebView) findViewById(R.id.wvSearchResult);
}
@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 search(View view){
String strURI = (etWord.getText().toString());
strURI = strURI.trim();
//如果查询内容为空提示
if (strURI.length() == 0)
{
Toast.makeText(MainActivity.this, "查询内容不能为空!", Toast.LENGTH_LONG)
.show();
}
//否则则以参数的形式从http://dict.youdao.com/m取得数据,加载到WebView里.
else
{
String strURL = "http://dict.youdao.com/m/search?keyfrom=dict.mindex&q="+ strURI;
wvSearchResult.loadUrl(strURL);
}
}
}调用有道词典查询单词
标签:
原文地址:http://www.cnblogs.com/wangyujin0313/p/4615354.html