服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android 有道词典的简单实现方法介绍

Android 有道词典的简单实现方法介绍

2021-01-09 17:00Android教程网 Android

本篇文章小编为大家介绍,Android 有道词典的简单实现方法介绍。需要的朋友参考下

首先看程序界面如下!

Android 有道词典的简单实现方法介绍

1、布局文件:

复制代码 代码如下:


<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

 

  <EditText
    android:id="@+id/editText"
    android:layout_width="150px"
    android:layout_height="40px"
    android:layout_x="5px"
    android:layout_y="32px"
    android:textSize="18sp" />

  <Button
    android:id="@+id/btnsearch"
    android:layout_width="60px"
    android:layout_height="40px"
    android:layout_x="165px"
    android:layout_y="35px"
    android:text="查询" />

  <Button
    android:id="@+id/btnclear"
    android:layout_width="60px"
    android:layout_height="40px"
    android:layout_x="230px"
    android:layout_y="35px"
    android:text="清空" />

  <WebView
    android:id="@+id/reswebView"
    android:layout_width="300px"
    android:layout_height="330px"
    android:layout_x="7px"
    android:layout_y="90px"
    android:focusable="false" />

</AbsoluteLayout>


2、修改MainActivity:

复制代码 代码如下:


public class MainActivity extends Activity {
 private Button btnSearch;
 private Button btnClear;
 private EditText editText;
 private WebView reswebView;

 

 private void SetView() {
  btnSearch = (Button) findViewById(R.id.btnsearch);
  btnClear = (Button) findViewById(R.id.btnclear);
  editText = (EditText) findViewById(R.id.editText);
  reswebView = (WebView) findViewById(R.id.reswebView);
  btnSearch.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    String strUri = editText.getText().toString();
    strUri = strUri.trim();
    if (strUri.length() == 0) {
     Toast.makeText(getApplicationContext(), "请输入查询字符", 1).show();
    } else {
     String strURL = "http://dict.youdao.com/m/search?keyfrom=dict.mindex&q=" + strUri;
     reswebView.loadUrl(strURL);
    }
   }
  });
  btnClear.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    editText.setText("");
   }
  });
 }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  SetView();
 }
}


3、在清单文件中添加网络访问权限:

 

<uses-permission android:name="android.permission.INTERNET" />

运行程序即可!

延伸 · 阅读

精彩推荐