12回答

0收藏

【三人行】第八篇、android蓝牙控制客户端

#竞赛 #竞赛 7691 人阅读 | 12 人回复 | 2013-09-06

android控制客户端程序
1、控制界面的布局
控制界面主要运用了线性布局、相对布局和表格布局。整体采用线性布局,局部采用相对布局,而控制按钮采用了表格布局。控制界面的布局如图1所示:

2、布局的代码如下:

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

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

    android:layout_width="wrap_content"

    android:layout_height="fill_parent"

    androidrientation="vertical" >

<RelativeLayout

   android:id = "@+id/container"

    androidrientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

   <RelativeLayout

      android:layout_width="fill_parent"

      android:layout_height="wrap_content"

      android:id= "@+id/edit_bottombar"

      android:layout_alignParentBottom= "true">

    <Button android:id="@+id/btn_disconnect"

            android:layout_width="65dp"

            android:layout_height="wrap_content"  

            android:layout_alignParentLeft ="true"

            android:text="断开"/>

        <Button android:id="@+id/btn_msg_send"

            android:layout_width="65dp"

            android:layout_height="wrap_content"  

            android:layout_alignParentRight ="true"

            android:text="发送"/>

        

        <EditText

            android:id="@+id/MessageText"

            android:layout_width="98dp"

            android:layout_height="wrap_content"

            android:layout_toRightOf="@+id/btn_disconnect"

            android:hint="说点什么呢?"

            android:textSize="15dip"

              />

    </RelativeLayout>  

     <ListView

         android:id="@+id/list"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:layout_above="@id/edit_bottombar"

         android:layout_below="@id/container"

         android:layout_weight="1.0"

         android:divider="#ffc6c6c6"

         android:scrollingCache="false"

         android:visibility="visible"/>


     <TableLayout

         android:layout_width="match_parent"

         android:layout_height="wrap_content">


         <TableRow

             android:id="@+id/tableRow1"

             android:layout_width="wrap_content"

             android:layout_height="wrap_content">


             <Button

                 android:id="@+id/button1"

                 android:layout_width="wrap_content"

                 android:layout_height="wrap_content"

                 android:text="Button"

                 android:visibility="invisible"/>


             <Button

                 android:id="@+id/start"

                 android:layout_width="wrap_content"

                 android:layout_height="wrap_content"

                 android:layout_marginRight="0dp"

                 android:text="start"

                 android:width="120px"/>

         </TableRow>


         <TableRow

             android:id="@+id/tableRow2"

             android:layout_width="wrap_content"

             android:layout_height="0dp"

             android:layout_weight="1">


             <Button

                 android:id="@+id/left"

                 android:layout_width="wrap_content"

                 android:layout_height="wrap_content"

                 android:layout_marginRight="0dp"

                 android:text="left"

                 android:width="120px"/>


             <Button

                 android:id="@+id/stop"

                 android:layout_width="wrap_content"

                 android:layout_height="wrap_content"

                 android:text="stop"

                 android:width="120px"/>


             <Button

                 android:id="@+id/right"

                 android:layout_width="wrap_content"

                 android:layout_height="wrap_content"

                 android:text="right"

                 android:width="120px"/>

         </TableRow>


         <TableRow

             android:id="@+id/tableRow3"

             android:layout_width="wrap_content"

             android:layout_height="wrap_content">


             <Button

                 android:id="@+id/button2"

                 android:layout_width="wrap_content"

                 android:layout_height="wrap_content"

                 android:text="Button"

                 android:visibility="invisible"/>


             <Button

                 android:id="@+id/back"

                 android:layout_width="wrap_content"

                 android:layout_height="wrap_content"

                 android:text="Back"/>

         </TableRow>

     </TableLayout>

</RelativeLayout>

</LinearLayout>

3、android客户端的界面如图2所示:

4、发送按钮的代码

sendButton=(Button)findViewById(R.id.btn_msg_send);

sendButton.setOnClickListener(new OnClickListener() {

@Override

public voidonClick(View arg0) {

// TODO Auto-generated method stub

String msgText =editMsgView.getText().toString();//获取编辑框内的内容

if (msgText.length()>0) {

    sendMessageHandle(msgText);//发送编辑框的内容给串口   

    editMsgView.setText("");//清空编辑框

    editMsgView.clearFocus();

//close InputMethodManager

InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

            imm.hideSoftInputFromWindow(editMsgView.getWindowToken(), 0);

}

else

Toast.makeText(mContext, "发送内容不能为空!", Toast.LENGTH_SHORT).show();

}

});

5、控制按钮的代码
以左转按钮为例:
sendButton=(Button)findViewById(R.id.left);
sendButton.setOnClickListener(new OnClickListener() {
@Override
public voidonClick(View arg0) {
String msgText ="1"; // 发送左转命令“l”
if (msgText.length()>0) {
sendMessageHandle(msgText);//发送“l”给串口
editMsgView.setText("");//清空编辑框
editMsgView.clearFocus();
//close InputMethodManager
InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editMsgView.getWindowToken(), 0);
}
else
Toast.makeText(mContext, "发送内容不能为空!", Toast.LENGTH_SHORT).show();
}
});

控制界面布局.jpg (39.69 KB, 下载次数: 66)

图1 控制界面的布局

图1 控制界面的布局

Screenshot_2013-09-06-22-09-40.png (46.9 KB, 下载次数: 77)

图2 android控制界面

图2 android控制界面
分享到:
回复

使用道具 举报

回答|共 12 个

倒序浏览

沙发

xukaikai_3004358

发表于 2013-9-6 22:30:30 | 只看该作者

很好,很前端,顶顶
板凳

anti-t

发表于 2013-9-7 07:20:09 | 只看该作者

不错哦----------------------
地板

fj1161

发表于 2013-9-7 08:16:26 | 只看该作者

牛逼啊~~~~~~~~~~
第五年了,这次一定要坚持到底,不留遗憾!!!
5#

jinshaomin

发表于 2013-9-7 16:53:01 | 只看该作者

xukaikai 发表于 2013-9-6 22:30
很好,很前端,顶顶

谢谢啊啊啊啊啊啊啊啊
6#

jinshaomin

发表于 2013-9-7 16:53:40 | 只看该作者

fj1161 发表于 2013-9-7 08:16
牛逼啊~~~~~~~~~~

过奖了
7#

jinshaomin

发表于 2013-9-7 16:54:15 | 只看该作者

吖坨 发表于 2013-9-7 07:20
不错哦----------------------

谢谢了啊啊
8#

xxn599

发表于 2013-9-8 09:51:06 | 只看该作者

嗯,毫无意义的代码,完全不涉及蓝牙部分
9#

jinshaomin

发表于 2013-9-8 10:53:41 | 只看该作者

xxn59 发表于 2013-9-8 09:51
嗯,毫无意义的代码,完全不涉及蓝牙部分

嗯,我会后续贴上
10#

楞伽_山人_3003203

发表于 2013-9-12 09:08:59 | 只看该作者

{:soso_e179:}{:soso_e179:}{:soso_e179:}{:soso_e179:}{:soso_e179:}{:soso_e179:}
您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条