本软件用了三个控件EditText,Button,WebView
布局文件界面:
4
运行结果:
布局文件代码:
<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:background="@drawable/mainbg"
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" >
<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:background="@drawable/bg_roundcorner"
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_alignLeft="@+id/btnClear"
android:layout_alignParentTop="true"
android:background="@drawable/ibsearchword"
android:onClick="search" />
<EditText
android:id="@+id/etWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btnSearch"
android:layout_marginTop="22dp"
android:layout_toLeftOf="@+id/btnClear"
android:background="@android:drawable/edit_text"
android:ems="10"
android:singleLine="true"
android:textColor="#552006"
android:textColorHint="#782f10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/btnSearch"
android:text="清空" />
</RelativeLayout>
MainActivity.java 代码:
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
//查询按钮申明
private Button btnSearch;
//清空按钮申明
private Button btnClear;
//输入框申明
private EditText editText;
//加载数据的WebView申明
private WebView webView;
private void SetView() {
btnSearch=(Button) findViewById(R.id.btnSearch);
//获取布局的几个控件
btnClear=(Button) findViewById(R.id.btnClear);
editText=(EditText) findViewById(R.id.etWord);
webView=(WebView) findViewById(R.id.wvSearchResult);
//查询按钮添加事件
btnSearch.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
String strURI = (editText.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;
webView.loadUrl(strURL);
webView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return false;
}
});
}
}
});
//清空按钮添加事件,将EditText置空
btnClear.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
editText.setText("");
}
});
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SetView();
}
}
注意:设置网络权限部分:
<uses-permission android:name="android.permission.INTERNET"/>