有道词典
来源:互联网 发布:linux ping n 编辑:程序博客网 时间:2024/06/10 06:00
Android应用实例,有道词典
实验步骤:
1.UI界面设计:
####从界面上看只有三个控件EditText,Button,WebView,我们用到的还包括一个Toast的控件,Toast控件是用来提示用户输入信息为空时或输入错误单词时的提示。我们在EditText输入查询内容,这里包括中文,英文。,点击查询按钮时,通过参数的形式,从http://dict.youdao.com/m取出数据把结果存放在WebView里。点击清空。UI界面代码:
<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" > <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> <Button android:id="@+id/btnSearch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/tvSearchResult" android:layout_alignTop="@+id/etWord" android:background="@drawable/ibsearchword" android:onClick="search" /> <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" /></RelativeLayout>
2.点击查询按钮,触发事件
#####先获取输入文本框中的单词信息,对获取的信息进行判断,如果输入的信息为空,则弹出提示“查询内容为空”,如果输入了单词或汉字,则通过以参数的形式从http://dict.youdao.com/m取得数据,加载到WebView里。
//查询按钮添加事件 myButton01.setOnClickListener(new Button.OnClickListener() { public void onClick(View arg0) { String strURI = (mEditText1.getText().toString()); strURI = strURI.trim(); //如果查询内容为空提示 if (strURI.length() == 0) { Toast.makeText(YouDao.this, "查询内容不能为空!", Toast.LENGTH_LONG) .show(); } else { String strURL = "http://dict.youdao.com/m/search?keyfrom=dict.mindex&q=" + strURI; mWebView1.loadUrl(strURL); } } });
3.点击清空按钮,将输入文本框中的信息清除
通过将获取的文本框的内容设置为空,实现文本框的清除功能
//清空按钮添加事件,将EditText置空 myButton02.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { mEditText1.setText(""); } }); }
MainActuvity所有代码:
package AndroidApplication.Instance;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.webkit.WebView;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class YouDao extends Activity{ //查询按钮申明 private Button myButton01; //清空按钮申明 private Button myButton02; //输入框申明 private EditText mEditText1; //加载数据的WebView申明 private WebView mWebView1; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获得布局的几个控件 myButton01 = (Button)findViewById(R.id.myButton01); myButton02 = (Button) findViewById(R.id.myButton02); mEditText1 = (EditText) findViewById(R.id.myEditText1); 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(YouDao.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(""); } }); }}
0 0
- 有道词典
- 有道词典
- 有道词典
- 有道词典
- 有道词典
- 有道词典
- 有道词典
- 有道词典
- 有道词典
- 有道词典
- 有道词典
- 有道词典
- 有道词典
- 有道词典
- 有道词典
- 有道词典
- 有道词典
- 有道词典
- 部署tomcat的war包脚本
- 98.根据公式求值
- IIS经典模式对ASP.NET MVC应用程序的影响
- Python学习之路7-3
- UI_拖动View
- 有道词典
- SqlServer 垂直分表
- myeclipse快捷键
- Autofac官方文档翻译——(一)Getting Started
- GRE写作必备句型
- Normalizer Transformation
- Google浏览器的红杏插件使用方法
- 通过weka.jar包来进行数据预处理
- 2012年5月SAT香港真题解析