聊天界面

来源:互联网 发布:淘宝网店铺排行榜 编辑:程序博客网 时间:2024/04/28 08:52

运行结果:

  

下面是res/layout/activity_main.xml 布局文件:

[java] view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#d8e0e8"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <ListView  
  9.         android:id="@+id/msg_list_view"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="0dp"  
  12.         android:layout_weight="1"  
  13.         android:divider="#000" />  
  14.   
  15.     <LinearLayout  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content" >  
  18.   
  19.         <EditText  
  20.             android:id="@+id/input_text"  
  21.             android:layout_width="0dp"  
  22.             android:layout_height="wrap_content"  
  23.             android:layout_weight="1"  
  24.             android:hint="Type somthing here"  
  25.             android:maxLines="2" />  
  26.   
  27.         <Button  
  28.             android:id="@+id/send"  
  29.             android:layout_width="wrap_content"  
  30.             android:layout_height="wrap_content"  
  31.             android:text="Send" />  
  32.     </LinearLayout>  
  33.   
  34. </LinearLayout>  

Android:divider指定ListView分隔线的颜色,这里设为透明色。


下面是res/layout/msg_item.xml 布局文件:

[java] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     android:padding="10dp" >  
  7.   
  8.     <LinearLayout  
  9.         android:id="@+id/left_layout"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_gravity="left"  
  13.         android:background="@drawable/message_left" >  
  14.   
  15.         <TextView  
  16.             android:id="@+id/left_msg"  
  17.             android:layout_width="wrap_content"  
  18.             android:layout_height="wrap_content"  
  19.             android:layout_gravity="center"  
  20.             android:layout_margin="10dp"  
  21.             android:textColor="#fff" />  
  22.     </LinearLayout>  
  23.   
  24.     <LinearLayout  
  25.         android:id="@+id/right_layout"  
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content"  
  28.         android:layout_gravity="right"  
  29.         android:background="@drawable/message_right" >  
  30.   
  31.         <TextView  
  32.             android:id="@+id/right_msg"  
  33.             android:layout_width="wrap_content"  
  34.             android:layout_height="wrap_content"  
  35.             android:layout_gravity="center"  
  36.             android:layout_margin="10dp" />  
  37.     </LinearLayout>  
  38.   
  39. </LinearLayout>  

下面是Msg.Java文件:

[java] view plain copy
  1. //定义消息的实体类  
  2. public class Msg {  
  3.     public static final int TYPE_RECEIVED=0;//表示这是一条收到的消息  
  4.     public static final int TYPE_SENT=1;//表示这是一条发送消息  
  5.     private String content;//消息的内容  
  6.     private int type;//消息的类型  
  7.       
  8.     public Msg(String content,int type) {  
  9.         this.content=content;  
  10.         this.type=type;  
  11.     }  
  12.   
  13.     public String getContent() {  
  14.         return content;  
  15.     }  
  16.   
  17.     public int getType() {  
  18.         return type;  
  19.     }  
  20.   
  21. }  


下面是MsgAdapter.java文件:

[java] view plain copy
  1. public class MsgAdapter extends ArrayAdapter<Msg>{  
  2.     private int resource;  
  3.   
  4.     //调用三个参数的构造方法  
  5.     public MsgAdapter(Context context, int resource, List<Msg> objects) {  
  6.         super(context, resource, objects);  
  7.         this.resource=resource;  
  8.     }  
  9.       
  10.     // 在每個子項被滾動到屏幕內的時候會被調用    
  11.     public View getView(int position, View convertView, ViewGroup parent) {  
  12.         Msg msg=getItem(position);  
  13.         View view;  
  14.         ViewHolder viewHolder ;  
  15.           
  16.         if(convertView==null){  
  17.             //根据自定义的R.layout.msg_item布局加载布局    
  18.             view=LayoutInflater.from(getContext()).inflate(resource, null);  
  19.             viewHolder=new ViewHolder();  
  20.             //得到各个控件的对象   
  21.             viewHolder.leftLayout=(LinearLayout) view.findViewById(R.id.left_layout);  
  22.             viewHolder.rightLayout=(LinearLayout) view.findViewById(R.id.right_layout);  
  23.             viewHolder.leftMsg=(TextView) view.findViewById(R.id.left_msg);  
  24.             viewHolder.rightMsg=(TextView) view.findViewById(R.id.right_msg);  
  25.             view.setTag(viewHolder);  
  26.         }else{  
  27.             view=convertView;  
  28.             viewHolder=(ViewHolder) view.getTag();  
  29.         }  
  30.           
  31.         //如果是收到的消息,则显示左边的消息布局,将右边的消息布局隐藏  
  32.         if(msg.getType()==Msg.TYPE_RECEIVED){  
  33.             viewHolder.leftLayout.setVisibility(View.VISIBLE);  
  34.             viewHolder.rightLayout.setVisibility(View.GONE);  
  35.             viewHolder.leftMsg.setText(msg.getContent());  
  36.         }else if(msg.getType()==Msg.TYPE_SENT){  
  37.             //如果是发出的消息,则显示右边的消息布局,将左边的消息布局隐藏  
  38.             viewHolder.leftLayout.setVisibility(View.GONE);  
  39.             viewHolder.rightLayout.setVisibility(View.VISIBLE);  
  40.             viewHolder.rightMsg.setText(msg.getContent());  
  41.         }  
  42.           
  43.         return view;  
  44.     }  
  45.       
  46.     //用于對控件 的實例進行緩存,优化我们每次在getView()方法中避免每次都调用View的findViewById()方法来获取控件的实例   
  47.     class ViewHolder{  
  48.         LinearLayout leftLayout;  
  49.         LinearLayout rightLayout;  
  50.         TextView leftMsg;  
  51.         TextView rightMsg;  
  52.     }  
  53.   
  54. }  

下面是MainActivity.java主界面文件:

[java] view plain copy
  1. public class MainActivity extends Activity {  
  2.     private ListView msgListView;  
  3.     private EditText inputText;  
  4.     private Button send;  
  5.     private MsgAdapter adapter;  
  6.     private List<Msg> msgList=new ArrayList<Msg>();  
  7.       
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_main);  
  11.         initMsgs();  
  12.         initViews();  
  13.     }  
  14.       
  15.     //初始化控件  
  16.     private void initViews(){  
  17.         adapter=new MsgAdapter(MainActivity.this, R.layout.msg_item, msgList);  
  18.         inputText=(EditText) findViewById(R.id.input_text);  
  19.         send=(Button) findViewById(R.id.send);  
  20.         msgListView=(ListView) findViewById(R.id.msg_list_view);  
  21.         msgListView.setAdapter(adapter);  
  22.           
  23.         send.setOnClickListener(new OnClickListener() {  
  24.             public void onClick(View v) {  
  25.                 String content=inputText.getText().toString();  
  26.                 if(!"".equals(content)){  
  27.                     Msg msg=new Msg(content,Msg.TYPE_SENT);  
  28.                     msgList.add(msg);  
  29.                     //当有新消息时,刷新ListView中的显示  
  30.                     adapter.notifyDataSetChanged();                   
  31.                     //将ListView定位到最后一行  
  32.                     msgListView.setSelection(msgList.size());  
  33.                     //清空输入框中的内容  
  34.                     inputText.setText("");  
  35.                 }  
  36.             }  
  37.         });  
  38.     }  
  39.       
  40.     //初始化消息数据  
  41.     private void initMsgs(){  
  42.         Msg msg1=new Msg("Hello dan.",Msg.TYPE_RECEIVED);  
  43.         msgList.add(msg1);  
  44.         Msg msg2=new Msg("Hello.Who is that?",Msg.TYPE_SENT);  
  45.         msgList.add(msg2);  
  46.         Msg msg3=new Msg("This is jin.Nice talking to you",Msg.TYPE_RECEIVED);  
  47.         msgList.add(msg3);  
  48.     }  
  49. }  


Take your time and enjoy it

0 0
原创粉丝点击