本次我们自己来编写一个聊天界面练习一下~
在主界面我们需要一个Listview现实对话内容,一个EditText输入文字,还需要一个Button来发送内容。
下面修改main_activity的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/msg_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:divider="#0000" >
ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/Type something"
android:maxLines="2" />
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@stirng/Send" />
LinearLayout>
LinearLayout>
这里android:divider是指定分割线颜色,#0000是透明色
建立实体类Msg
public class Msg {
public static final int TYPE_RECEIVED = 0;//表示这是一条接收到的消息
public static final int TYPE_SEND = 1;//表示这是一条发送的消息
private String content;
private int type;
public Msg(String content, int type) {
this.content = content;
this.type = type;
}
public String getContent(){
return content;
}
public int getType(){
return type;
}
}
接下来编写Listview的子布局,新建msg_item.xml
<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="10dp"
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="10dp"
android:textColor="#fff" />
LinearLayout>
LinearLayout>
让接收到的消息左对齐,发送的消息右对齐。将背景设置成我们准备好的图片并用Nine-Patch处理好。(不会Nine-Patch的童鞋请戳这里http://blog.csdn.net/fad_further/article/details/44358631)
你可能会问,把发送消息和接收消息都放在一个文件里尊的可以吗?
尊的是可以的。我们可以通过设置visible属性来决定显示哪个。
接下来,就要写适配器了。创建一个MsgAdapter,继承自ArrayAdapter
public class MsgAdapter extends ArrayAdapter{
private int resourceId;
public MsgAdapter(Context context, int textViewResourceId, List objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
resourceId = textViewResourceId;
}
public View getView(int postion, View convertView, ViewGroup parent){
Msg msg = getItem(postion);
View view;
ViewHolder viewHolder;
if(convertView == null){
view = LayoutInflater.from(getContext()).inflate(resourceId, 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_SEND){
viewHolder.leftLayout.setVisibility(View.GONE);
viewHolder.rightLayout.setVisibility(View.VISIBLE);
viewHolder.rightMsg.setText(msg.getContent());
}
return view;
}
class ViewHolder{
LinearLayout leftLayout, rightLayout;
TextView leftMsg, rightMsg;
}
}
可以看到,我们在getView方法中增加了对消息类型的判断。除此之外,就和我们在笔记6中写的一样啦~
最后我们在MainActivity方法中添加如下代码,做一些初始化工作和按钮的响应事件。
public class MainActivity extends ActionBarActivity {
private ListView msgListView;
private EditText inputText;
private Button send;
private MsgAdapter adapter;
private List msgList = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
adapter = new MsgAdapter(MainActivity.this, R.layout.msg_item, msgList);
inputText = (EditText)findViewById(R.id.input);
send = (Button)findViewById(R.id.send);
msgListView = (ListView)findViewById(R.id.msg_list);
msgListView.setAdapter(adapter);
send.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String content = inputText.getText().toString();
if(!"".equals(content)){
Msg msg = new Msg(content, Msg.TYPE_SEND);
msgList.add(msg);
adapter.notifyDataSetChanged();//有消息就刷新ListView的显示
msgListView.setSelection(msgList.size());//定位到ListView的最后一行,以保证一定可以看到最后一条发出的消息
inputText.setText(""); //清空输入框
}
}
});
}
void init(){
Msg msg1 = new Msg("Hi further~", Msg.TYPE_RECEIVED);
msgList.add(msg1);
Msg msg2 = new Msg("Hello~", Msg.TYPE_SEND);
msgList.add(msg2);
Msg msg3 = new Msg("I like you blog very much!", Msg.TYPE_RECEIVED);
msgList.add(msg3);
}
}
这样所有的工作就完成啦!是不是木有很难呢?
迫不及待的run一下
是不是还有点小激动呢~╰( ̄▽ ̄)╭
下载Demo请戳这里http://download.csdn.net/detail/u013750822/8519343