关于百度词典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:取消软键盘的显示

  代码如下:

利用百度词典API和Volley网络库开发的android词典应用
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 Studio XML布局基础
  2. Android studio的常见布局
  3. Android:布局(线性布局LinearLayout)
  4. Android高手应该精通哪些内容?
  5. android使用HttpClient和URLConnection获取网页内容
  6. 【Android】实现动态显示隐藏密码输入框的内容
  7. Android-Layout布局

随机推荐

  1. android控件的对齐方式
  2. android平板上的GridView视图缓存优化
  3. Android开发之旅:活动与任务
  4. Android中的style&themes
  5. Android(安卓)Studio使用教程、工程目录
  6. 70个具有商业实战性的精品Android源码
  7. Android高斯模糊、高斯平滑(Gaussian Blur
  8. Android与H5相机、相册笔记
  9. JPCT-AE for Android(安卓)3D (一)------
  10. Android内存泄露调试分享