您的位置:首页 > 移动开发 > Android开发

Android基于基于布局嵌套的页面导航实现

2014-01-09 20:25 501 查看
页面如下:



主页面的布局分隔为三部分:

Xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gztab_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- header  -->
<include android:id="@+id/gz_top" layout="@layout/tab_top"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

<!-- content  -->
<TabHost
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TabWidget
android:id="@+id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TabWidget>

<FrameLayout
android:id="@+id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/unhanlderLayout">
<Button android:id="@+id/gzbtn_unhandler_refresh"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="刷新"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView android:id="@+id/gz_unhandler_ListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/handlingLayout">
<Button android:id="@+id/gzbtn_handlering_refresh"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="刷新"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView android:id="@+id/gz_handleringListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/handledLayout">
<Button android:id="@+id/gzbtn_handled_refresh"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="刷新"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView android:id="@+id/gzgzbtn_handled_ListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
<!-- bottom  -->
<Button android:id="@+id/btnManyInfo" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="更多(10)"/>
</LinearLayout>


注意观察上面标记为红色的android:id均采用android系统默认的名称:

页面的导航组件:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout  android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/top_gzbtn_left"
android:text="返回"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12.0dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<Button
android:id="@+id/top_gzbtn_right"
android:text="发布"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="12.0dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
<TextView android:textSize="22.0sp"
android:textColor="#ffffffff"
android:ellipsize="middle"
android:gravity="center_horizontal"
android:id="@+id/top_gztxt_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="故障管理" android:singleLine="true" android:layout_toLeftOf="@+id/top_gzbtn_right" android:layout_toRightOf="@+id/top_btn_left0" android:layout_centerInParent="true" android:layout_alignWithParentIfMissing="true" >
</TextView>
</RelativeLayout>


上面红色布局主要采用相对布局定位相关的导航位置:

每一个tab页面的中的内容如下:

Xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<!-- 个人信息 -->

<LinearLayout
android:id="@+id/gzPersonInfoLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<ImageView
android:id="@+id/gzimg_item_Img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_margin="5px"
android:src="@drawable/ic_launcher" />

<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:shrinkColumns="1" >

<TableRow>

<TextView
android:id="@+id/gztxt_item_userCode"
android:padding="3dip" />

<TextView
android:id="@+id/gztxt_item_UserCollectionName"
android:padding="3dip" />

<TextView
android:id="@+id/gztxt_item_Time"
android:padding="3dip" />
</TableRow>

<TableRow>

<TextView
android:id="@+id/gztxt_item_name_person"
android:padding="3dip" />

<TextView
android:padding="3dip"
android:text="" />

<TextView android:padding="3dip" />
</TableRow>
</TableLayout>
</LinearLayout>

<LinearLayout
android:id="@+id/gzItemLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TableRow>

<TextView
android:padding="3dip"
android:text="项目名称:" />

<TextView
android:id="@+id/gztxt_item_name"
android:padding="3dip" />

<TextView
android:padding="3dip"
android:text="列车编号:" />

<TextView
android:id="@+id/gztxt_item_code"
android:padding="3dip" />
</TableRow>
</TableLayout>
</LinearLayout>

<LinearLayout
android:id="@+id/gzItemLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:shrinkColumns="1" >

<TableRow>

<TextView
android:padding="3dip"
android:text="故障负责人" />

<TextView
android:id="@+id/gztxt_item_user"
android:padding="3dip" />
</TableRow>

<TableRow>

<TextView
android:padding="3dip"
android:text="故障处理人" />

<TextView
android:id="@+id/gztxt_item_handler"
android:padding="3dip" />
</TableRow>

<TableRow>

<TextView
android:padding="3dip"
android:text="故障描述" />

<TextView
android:id="@+id/gztxt_item_descption"
android:padding="3dip" />
</TableRow>

<TableRow>

<TextView
android:padding="3dip"
android:text="问题级别:" />

<TextView
android:id="@+id/gztxt_item_level"
android:padding="3dip"
android:text="一般" />
</TableRow>

<TableRow>

<TextView
android:padding="3dip"
android:text="状态:" />

<TextView
android:id="@+id/gztxt_item_status"
android:padding="3dip"
android:text="未解决" />
</TableRow>
</TableLayout>
</LinearLayout>

</LinearLayout>


讯飞的语音sdk是需要申请的,地址是:http://dev.voicecloud.cn/developer.php?vt=1 。申请一个讯飞的开发者账号,然后申请一个appid,申请的时候需要填写开发者信息和你的应用的信息。

申请之后经过审核通过,会得到一个appid,这个是在使用语音服务时需要用到的。(笔者感觉申请还是比较容易通过的,简单地填写一下就通过了。速度也很快,我在晚上十一点多申请的,到第二天早上九点多就收到审核通过的邮件。个人感觉这个审核只是为了防止恶意使用,毕竟语音服务是要使用讯飞的服务器资源的。)

申请到appid之后就可以下载SDK了,然后使用语音服务了。

以下我试用的过程,(点击button,弹出语音框,说完之后将识别的结果显示在EditText中):

在eclipse里新建一个android工程

导入讯飞的语音jar包

讯飞的服务是需要一堆权限的,在manifest.xml中加入

[html] view plaincopyprint?

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

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

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

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

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

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

Html代码


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

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

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

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

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

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

分别为:通过麦克风录音、联网、获取网络信息状态、获取wifi状态、改变网络状态如是否能联网、访问电话状态

在main.xml中添加一个EditText和一个Button,如下

[html] view plaincopyprint?

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<EditText

android:id="@+id/editText"

android:layout_width="fill_parent"

android:layout_height="300dp"

android:gravity="top"

android:inputType="textMultiLine" >

<requestFocus />

</EditText>

<Button

android:id="@+id/button_start"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="点击开始说话" />

</LinearLayout>

Html代码


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<EditText

android:id="@+id/editText"

android:layout_width="fill_parent"

android:layout_height="300dp"

android:gravity="top"

android:inputType="textMultiLine" >

<requestFocus />

</EditText>

<Button

android:id="@+id/button_start"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="点击开始说话" />

</LinearLayout>

然后在MainActivity中编写代码实现了

(这里可以看到在线的文档:http://dev.voicecloud.cn/developer.php?category=YW5kcm9pZA%3D%3D&column=ZG9jdW1lbnQ%3D&type=d2lkZ2V0

通过阅读讯飞给的文档,可以发现标准的识别控件是RecognizerDialog——是一个Dialog的子类,所以我们是可以在Activity中通过showDialog(int)方法来调用它的。

重写Activity的方法

[java] view plaincopyprint?

@Override

protected Dialog onCreateDialog(int id) {}

Java代码


@Override

protected Dialog onCreateDialog(int id) {}

在其中创建并设置好一个RecognizerDialog即可。

创建RecognizerDialog方法为

[java] view plaincopyprint?

RecognizerDialog recognizerDialog = new RecognizerDialog(

MainActivity.this, "appid=1234567");// 这里应该写从科大讯飞申请到的appid

Java代码


RecognizerDialog recognizerDialog = new RecognizerDialog(

MainActivity.this, "appid=1234567");// 这里应该写从科大讯飞申请到的appid

(其中appid应该写自己从讯飞申请到的appid,由于协议的问题,我不便把自己申请的id公开,所以这里用了1234567来代替。根据讯飞的说明,非法的appid是不能使用语音服务的,但是我用随机数字试验了一下,居然也是能用的,不知道是不是BUG。)

官方的文档:



然后需要设置识别参数

[java] view plaincopyprint?

recognizerDialog.setEngine("sms", null, null);

Java代码


recognizerDialog.setEngine("sms", null, null);

第一个参数“sms”表示为请求的服务为“语音识别”。后两个参数暂时设为null就好。

官方文档:



最后还需给recognizerDialog设置一个listener,回调接口用以获取结果,

recognizerDialog.setListener(RecognizerDialogListener listener)的参数为RecognizerDialogListener接口,实现此接口要重写两个方法:onResults(ArrayList results,boolean isLast)和onEnd(SpeechError error)。用以获取和处理结果。

我的实现为直接写了一个匿名类:

[java] view plaincopyprint?

recognizerDialog.setListener(new RecognizerDialogListener() {

@Override

public void onResults(ArrayList<RecognizerResult> results,

boolean arg1) {

StringBuffer result = new StringBuffer();

for (RecognizerResult r : results) {

result.append(r.text);

}

editText.setText(result.toString());

}

@Override

public void onEnd(SpeechError arg0) {

}

});

Java代码


recognizerDialog.setListener(new RecognizerDialogListener() {

@Override

public void onResults(ArrayList<RecognizerResult> results,

boolean arg1) {

StringBuffer result = new StringBuffer();

for (RecognizerResult r : results) {

result.append(r.text);

}

editText.setText(result.toString());

}

@Override

public void onEnd(SpeechError arg0) {

}

});

然后将此RecognizerDialog返回即可。

完整的MainActivity代码:

[java] view plaincopyprint?

import com.iflytek.speech.RecognizerResult;

import com.iflytek.speech.SpeechError;

import com.iflytek.ui.RecognizerDialog;

import com.iflytek.ui.RecognizerDialogListener;

public class MainActivity extends Activity {

EditText editText = null;

Button startButton = null;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

editText = (EditText) findViewById(R.id.editText);

startButton = (Button) findViewById(R.id.button_start);

startButton.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

showDialog(1);

}

});

}

@Override

protected Dialog onCreateDialog(int id) {

RecognizerDialog recognizerDialog = new RecognizerDialog(

MainActivity.this, "appid=1234567");// 这里应该写从科大讯飞申请到的appid

recognizerDialog.setEngine("sms", null, null);

recognizerDialog.setListener(new RecognizerDialogListener() {

@Override

public void onResults(ArrayList<RecognizerResult> results,

boolean arg1) {

StringBuffer result = new StringBuffer();

for (RecognizerResult r : results) {

result.append(r.text);

}

editText.setText(result.toString());

}

@Override

public void onEnd(SpeechError arg0) {

}

});

return recognizerDialog;

}

}

Java代码


import com.iflytek.speech.RecognizerResult;

import com.iflytek.speech.SpeechError;

import com.iflytek.ui.RecognizerDialog;

import com.iflytek.ui.RecognizerDialogListener;

public class MainActivity extends Activity {

EditText editText = null;

Button startButton = null;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

editText = (EditText) findViewById(R.id.editText);

startButton = (Button) findViewById(R.id.button_start);

startButton.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

showDialog(1);

}

});

}

@Override

protected Dialog onCreateDialog(int id) {

RecognizerDialog recognizerDialog = new RecognizerDialog(

MainActivity.this, "appid=1234567");// 这里应该写从科大讯飞申请到的appid

recognizerDialog.setEngine("sms", null, null);

recognizerDialog.setListener(new RecognizerDialogListener() {

@Override

public void onResults(ArrayList<RecognizerResult> results,

boolean arg1) {

StringBuffer result = new StringBuffer();

for (RecognizerResult r : results) {

result.append(r.text);

}

editText.setText(result.toString());

}

@Override

public void onEnd(SpeechError arg0) {

}

});

return recognizerDialog;

}

}

程序在真机运行截图,经过检验,科大讯飞的识别率还是很高的。

demo下载地址:http://download.csdn.net/detail/barryhappy/4178459
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: