关于百度词典API的说明,地址在这里:百度词典API介绍

  关于android网络库Volley的介绍说明,地址在这里:Android网络通信库Volley

  首先我们看下大体的界面布局!

  

  再帖张最终的效果图

  

  看到效果图,应该知道布局很简单了对吧:

  布局xml文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:orientation="vertical"    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="com.example.dict.MainActivity" >          <LinearLayout         android:orientation="horizontal"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        >                <Button             android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="Clear"            android:onClick="clearHandler"            android:padding="7dip"            />        <EditText            android:id="@+id/etWord"            android:singleLine="true"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="2"            android:background="@android:drawable/edit_text"             />        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="Query"            android:onClick="queryHandler"            android:padding="7dip"             />    </LinearLayout>    <TextView         android:id="@+id/tvResult"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:hint="result"        /></LinearLayout>

  然后就是几个事件的处理,

  1: Clear按钮的处理: 清理掉输入框里面的内容

  2: Query按钮的处理:

    1:利用百度词典API获取内容

    2:解析返回的json数据,处理并且显示在下面的TextView空间中,

    3:取消软键盘的显示

  代码如下:

package com.example.dict;import java.util.HashMap;import java.util.Map;import org.json.JSONArray;import org.json.JSONObject;import com.android.volley.Request;import com.android.volley.RequestQueue;import com.android.volley.Response;import com.android.volley.VolleyError;import com.android.volley.toolbox.StringRequest;import com.android.volley.toolbox.Volley;import android.support.v7.app.ActionBarActivity;import android.content.Context;import android.os.Bundle;import android.view.View;import android.view.inputmethod.InputMethodManager;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends ActionBarActivity {        private String url = "http://openapi.baidu.com/public/2.0/translate/dict/simple?client_id=5kHZHeo8MN7L6NmPTGV6POsb&q=@word&from=en&to=zh";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }        public void clearHandler(View view)    {        EditText text = (EditText)findViewById(R.id.etWord);        text.setText("");    }        public void queryHandler(View view)    {        EditText text = (EditText)findViewById(R.id.etWord);        if(text.getText().length() <= 0)        {            return ;        }                RequestQueue requestQueue = Volley.newRequestQueue(this);                String tempUrl = this.url.replace("@word",text.getText().toString());                StringRequest postRequest = new StringRequest(Request.Method.GET,tempUrl,                new Response.Listener<String>()                 {                    @Override                    public void onResponse(String response)                    {                        MainActivity.this.parseResult(response);                    }                },                new Response.ErrorListener()                 {                    @Override                    public void onErrorResponse(VolleyError error)                    {                                            }                })                {                @Override                protected Map<String,String> getParams()                {                    Map<String,String> params = new HashMap<String,String>();                    return params;                }};        requestQueue.add(postRequest);    }        public void parseResult(String source)    {        try        {                        final TextView display = (TextView)findViewById(R.id.tvResult);            display.setText("");                        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);             if (imm.isActive())             {                imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);            }                         JSONObject obj = new JSONObject(source);            JSONObject data = obj.getJSONObject("data");            JSONArray symbols = data.getJSONArray("symbols");                        for(int i=0;i<symbols.length();i++)            {                JSONObject parts = symbols.getJSONObject(i);                                for(int j=0;j < parts.getJSONArray("parts").length();j++)                {                    JSONObject item = parts.getJSONArray("parts").getJSONObject(j);                    display.append(item.getString("part"));                    display.append("\n");                    JSONArray arr = item.getJSONArray("means");                                        for(int k=0;k<arr.length();k++)                    {                        display.append("    " + arr.getString(k));                        display.append("\n");                    }                }            }        }        catch (Exception e)        {                    }    }    }
View Code

  

  需要改进的地方:

    1:做一个本地查询记录的存储

    2:解析内容做一个Scroll的控制

更多相关文章

  1. Android(安卓)中全局键的处理GlobalKeyManager
  2. Day1.5--Android简介之初识Activity
  3. [转]android的Goldfish内核概述
  4. Android之Goldfish
  5. Android消息处理机制——AsyncTask 源码解析
  6. android图片优化,内存释放处理
  7. android 关于焦点的获取与监听失效问题的处理
  8. Android图像处理之GPUImage for Android
  9. Android常见错误处理

随机推荐

  1. Android 框架层为IMountService 增加新接
  2. Android下junit单元测试、logCat的使用
  3. 推荐给Android开发者的七本图灵书
  4. 检查Android是否支持指纹识别以及是否已
  5. android默认允许usb调试和usb权限
  6. TextView 属性
  7. Android View 相关源码分析之四 LinearLa
  8. android studio适配器Adapter
  9. 最简单的BufferQueue测试程序(一)
  10. 20155323 第四次实验 Android程序设计实