安卓模拟聊天界面---改编第一行代码

千金一刻莫空度,老大无成空自伤。这篇文章主要讲述安卓模拟聊天界面---改编第一行代码相关的知识,希望能为你提供帮助。
主界面
 
< ?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d8e0e8"
android:orientation="vertical" >

< ListView
android:id="@+id/msg_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:divider="#000"> < /ListView>

< LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

< EditText
android:id="@+id/input_text"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="请输入聊天内容"/>

< Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送"> < /Button>



< /LinearLayout>

< /LinearLayout>
 
消息item
< ?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">

< LinearLayout
android:id="@+id/left_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:background="@drawable/message_left">

< TextView
android:id="@+id/left_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dip"
android:textColor="#fff"/>
< /LinearLayout>


< LinearLayout
android:id="@+id/right_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@drawable/message_right">

< TextView
android:id="@+id/right_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dip"
android:textColor="#fff"/>
< /LinearLayout>



< /LinearLayout>
 
消息类
 
【安卓模拟聊天界面---改编第一行代码】package com.example.thingking_in_android;
//消息类
public class Msg {
public staticboolean flag=true;

public static final int TYPE_RECEIVED=0;

public static final int TYPE_SENT=1;

private String content;
private int type;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public Msg(String content, int type) {
super();
this.content = content;
this.type = type;
}



}
 
主类
 
package com.example.thingking_in_android;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;
import com.example.thingking_in_android.R.layout;
import android.R.anim;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
/**
* 模拟聊天
* @author 黄二狗参考第一行代码改编
*
*/

public class HelloActivity extends Activity {
//聊天 列表视图
private ListView msgListView;

//输入信息框
private EditText inputText;

//发送按钮
private Button send;

List< Msg> list;

myAdapter adapter;



@Override
protected void onCreate(Bundle savedInstanceState) {

System.out.println("begin");
super.onCreate(savedInstanceState);
//不显示标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);

//加载布局文件
this.setContentView(R.layout.activity_main);

//设置数据
list=new ArrayList();
Msg msg1=new Msg("Hello I‘am jack!",Msg.TYPE_RECEIVED);
Msg msg2=new Msg("Hello are you ok?",Msg.TYPE_SENT);
Msg msg3=new Msg("Yes I am very ok!",Msg.TYPE_RECEIVED);
Msg msg4=new Msg("That‘s all right!",Msg.TYPE_SENT);
list.add(msg1);
list.add(msg2);
list.add(msg3);
list.add(msg4);


//获取相应的视图组件
inputText=(EditText)findViewById(R.id.input_text);
send=(Button)findViewById(R.id.send);
msgListView=(ListView)findViewById(R.id.msg_list_view);

adapter=new myAdapter();
System.out.println(adapter);
System.out.println(list);
msgListView.setAdapter(adapter);
//为发送按钮添加事件
send.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
//获取用户输入数据
String content=inputText.getText().toString();
//判断数据是否为空
if(!"".equals(content))
{
//模拟一发一收的情况

Msg msg;
if(Msg.flag)
{
msg=new Msg(content,Msg.TYPE_SENT);

}
else
{
msg=new Msg(content,Msg.TYPE_RECEIVED);
}

Msg.flag=!Msg.flag;
list.add(msg);

adapter.notifyDataSetChanged(); //当有新消息时 刷新ListView的显示

inputText.setText("");
}

}
});



}


class myAdapter extends BaseAdapter
{
@Override
public int getCount() {

return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {


System.out.println("加载===");
//加载布局 并设置数据


Msg msg=list.get(position);
View view;
viewHolder viewHolder;
//布局会重新加载 所以需要缓冲
if(convertView==null)
{
view=LayoutInflater.from(HelloActivity.this).inflate(R.layout.msg_item,null);
//将视图存储到对象中
viewHolder=new viewHolder();

viewHolder.leftLayout=(LinearLayout)view.findViewById(R.id.left_layout);
viewHolder.rightLayout=(LinearLayout)view.findViewById(R.id.right_layout);

viewHolder.leftMsg=(TextView)view.findViewById(R.id.left_msg);
viewHolder.rightMsg=(TextView)view.findViewById(R.id.right_msg);

//视图可以存储对象
view.setTag(viewHolder);

}
else
{
view=convertView;
viewHolder=(viewHolder)view.getTag();
}



//设置资源
if(msg.getType()==Msg.TYPE_RECEIVED)
{
viewHolder.leftLayout.setVisibility(View.VISIBLE);
viewHolder.rightLayout.setVisibility(View.GONE);
viewHolder.leftMsg.setText(msg.getContent());
}

else if (msg.getType()==Msg.TYPE_SENT)
{
viewHolder.rightLayout.setVisibility(View.VISIBLE);
viewHolder.leftLayout.setVisibility(View.GONE);
viewHolder.rightMsg.setText(msg.getContent());
}

return view;


}

}


}




































































































































































































































































































    推荐阅读