android控件之TextView显示聊天室信息
来源:互联网 发布:推广数据留言 编辑:程序博客网 时间:2024/05/21 14:34
TextView是一个完整的文本编辑器,但是基类为不允许编辑;其子类EditText允许文本编辑。TextView控件是android控件里面比较常用的了。
当我们做一个简易的聊天室时,用什么来显示聊天信息呐?使用Toast控件未免太不实际,毕竟它是一个消息提示框,不会得到焦点。
所以我们需要用到TextView来是实现。
同时也需要将textView 放在ScrollView内
使用ScrollView控件是为了防止信息过多,这样屏幕就可以滚动了。
xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#f0f0f0" ><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="OnMySelfClick1" android:text="发送" android:layout_x="40px" android:layout_y="50px" android:textColor="#ff0000" /> <ScrollView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <TextView android:id="@+id/txt" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="25px" /> </ScrollView></LinearLayout>
Client:
package A.B;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import java.net.*; //jar包import java.io.*; //jar包import static java.lang.System.in;public class MainActivity extends Activity{ public void OnMySelfClick1(View v) { // TODO Auto-generated method stub <span style="color:#ff0000;">String show; TextView txt; txt=(TextView)findViewById(R.id.txt);</span> try { // 创建一个Socket对象,并指定服务端的IP及端口号 Socket s =new Socket("192.168.186.46",3333); //输入Ip(随着局域网的改变,ip会改变,要输入目前的ip地址),和端口号 OutputStream os=s.getOutputStream(); //相当于一个输出管道,客户端必须要有一个接收管道(输入管道) DataOutputStream dos=new DataOutputStream(os); //输出流 dos.writeUTF("你好我是Client..."); DataInputStream dis= new DataInputStream(s.getInputStream());//输入流 <span style="color:#ff0000;">show = dis.readUTF(); // 对方发来的信息 if (show != null) { txt.setText(show); //将对方发来的信息显示在ID为txt的TextView上面 } else{ txt.setText("等待服务器回应..."); }</span> dos.close(); //关闭dos in.close(); s.close(); //关闭s } catch (IOException e) { } } }
package javaapplication1; import java.net.*; import java.io.*; public class Server { //类名 public static void main(String[] args) throws Exception /**抛出异常*/{ ServerSocket ss=new ServerSocket(3333); //定义服务器端口 System.out.println("服务器已经启动,等待连接...."); while(true) { //循环监听 Socket s = ss.accept(); //等待对方的连接 System.out.println("A connent"); //客户端连接后输出值 DataInputStream dis= new DataInputStream(s.getInputStream()); //接收管道(输入管道) System.out.println(dis.readUTF()); //对应客户端的dos.writeUTF("") 读的用dis写的用dos OutputStream os=s.getOutputStream(); //构建输出管道 DataOutputStream dos=new DataOutputStream(os); //输出流 dos.writeUTF("欢迎您的连接..."); //向客户端发送的信息 dis.close(); //关闭dis dos.close(); //关闭dos s.close(); //关闭s } }}
(注意Server是单独在PC上运行的,上面的xml和Client是在一个Android项目里面的)
(注意配置权限)<uses-permission android:name="android.permission.INTERNET"></uses-permission>
通过上面的代码就可以实现简单的聊天信息显示在TextView上面=============================================================================================================
关于在TextView里面设置网址的记录
<TextView
android:layout_width="fill_parent"
android:layout_height="45px"
android:background="#D15FEE"
android:textSize="25px"
android:text="www.baidu.com"
/>
这样设置的网址只是会显示文本,不能点击。
但是在xml布局文件里面加上 android:autoLink="all"就可以了
android:autoLink //设置是否显示为可点击的链接。可选值(none/web/email/phone/map/all)
<TextView
android:autoLink="all"
android:layout_width="fill_parent"
android:layout_height="45px"
android:background="#D15FEE"
android:textSize="25px"
android:text="www.baidu.com"
/>
建议参考
Android学习笔记10:TextView的使用
- android控件之TextView显示聊天室信息
- Android之TextView、EditText控件显示表情图片
- android控件之Textview
- android控件之TextView
- Android控件之TextView
- Android控件之Textview
- Android控件之TextView
- Android控件之Textview
- android控件之TextView
- Android控件之TextView
- Android之TextView(控件)
- android控件之TextView
- Android控件之TextView
- Android显示TextView文本控件
- android控件TextView之 分段显示不…
- Android自定义控件之《自定义TextView(支持显示图片)》
- Android控件开发之TextView
- Android控件之TextView探究
- 钓鱼
- 静态库与动态库
- springmvc前段字段和后台返回字段不一致报错
- Jquery 中 offset() 方法
- unity3d shader学习(2)-unity shader的结构
- android控件之TextView显示聊天室信息
- shell 关于取得参数的问题
- JAVA温习课堂13
- UML用例图
- 线程通信-等待和唤醒机制和锁(Lock)机制
- Java并发基础(五)-CountDownLatch、CyclocBarrier、Phaser的使用
- ImageLoader
- Java单例模式--饿汉式、懒汉式需要怎么写
- Almost Sorted Array(HDU 5532 单调递增子序列)