有道词典
2015-07-05 11:08
260 查看
package com.example.youdaodictionary;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
//查询按钮申明
private Button myButton01;
//清空按钮申明
private Button myButton02;
//输入框申明
private EditText mEditText1;
//加载数据的WebView申明
private WebView mWebView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获得布局的几个控件
myButton01 = (Button)findViewById(R.id.myButton01);
myButton02 = (Button) findViewById(R.id.myButton02);
mEditText1 = (EditText) findViewById(R.id.etWord);
mWebView1 = (WebView) findViewById(R.id.myWebView1);
//查询按钮添加事件
myButton01.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View arg0)
{
String strURI = mEditText1.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;
mWebView1.loadUrl(strURL);
}
}
});
//http://dict.youdao.com/m/search?keyfrom=dict.mindex&q=happy
//清空按钮添加事件,将EditText置空
myButton02.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
mEditText1.setText("");
}
});
}
@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;
}
}
布局文件:
<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" >
<Button
android:id="@+id/myButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/myButton02"
android:layout_alignTop="@+id/etWord"
android:background="@android:drawable/btn_default"
android:onClick="search"
android:text="查询"/>
<WebView
android:id="@+id/myWebView1"
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="@android:drawable/btn_default_small"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="25sp" />
<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="15dp"
android:layout_toLeftOf="@id/myButton01"
android:background="@android:drawable/edit_text"
android:ems="10"
android:singleLine="true"
android:textColor="#552006"
android:textColorHint="#782f10" >
<requestFocus />
</EditText>
<Button
android:id="@id/myButton02"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/myWebView1"
android:layout_alignParentRight="true"
android:text="清空" />
</RelativeLayout>
package com.example.youdaodictionary;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
//查询按钮申明
private Button myButton01;
//清空按钮申明
private Button myButton02;
//输入框申明
private EditText mEditText1;
//加载数据的WebView申明
private WebView mWebView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获得布局的几个控件
myButton01 = (Button)findViewById(R.id.myButton01);
myButton02 = (Button) findViewById(R.id.myButton02);
mEditText1 = (EditText) findViewById(R.id.etWord);
mWebView1 = (WebView) findViewById(R.id.myWebView1);
//查询按钮添加事件
myButton01.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View arg0)
{
String strURI = mEditText1.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;
mWebView1.loadUrl(strURL);
}
}
});
//http://dict.youdao.com/m/search?keyfrom=dict.mindex&q=happy
//清空按钮添加事件,将EditText置空
myButton02.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
mEditText1.setText("");
}
});
}
@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;
}
}
布局文件:
<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" >
<Button
android:id="@+id/myButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/myButton02"
android:layout_alignTop="@+id/etWord"
android:background="@android:drawable/btn_default"
android:onClick="search"
android:text="查询"/>
<WebView
android:id="@+id/myWebView1"
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="@android:drawable/btn_default_small"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="25sp" />
<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="15dp"
android:layout_toLeftOf="@id/myButton01"
android:background="@android:drawable/edit_text"
android:ems="10"
android:singleLine="true"
android:textColor="#552006"
android:textColorHint="#782f10" >
<requestFocus />
</EditText>
<Button
android:id="@id/myButton02"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/myWebView1"
android:layout_alignParentRight="true"
android:text="清空" />
</RelativeLayout>
相关文章推荐
- 51NOD算法马拉松 最大值问题 离线预处理+set lower_bound
- android动画之android:interpolator属性使用
- Bloom Filter布隆过滤器
- 需求与用户故事
- Leetcode_228_Summary Ranges
- Lettcode_228_Summary Ranges
- ubuntu14.04 安装 bcm43142无线网卡
- No Gambling - HUD 3863 博弈
- CentOS下搭建DNS服务器
- Linux命令-工作管理命令:&,ctrl+z,jobs,fg,bg
- OSPF协议总结
- PAT 1019 General Palindromic Number
- HDOJ-5277 YJC counts stars(DFS)
- android4.0Caused by: android.os.NetworkOnMainThreadException问题
- javax.xml.ws.WebServiceException: No Content-type in the header!
- 磁盘配额、例行性工作、系统服务-(Linux笔记)
- Webview获取连续H5页面的标题
- equals方法
- 工作管理、进程管理-(Linux笔记)
- Android IOS WebRTC 音视频开发总结(三二)-- WebRTC项目开发建议