一个自己和自己说话的小程序

首先在layout里写布局

activity_main.xml代码如下


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    tools:context=".MainActivity" >
	<ListView 
        android:id="@+id/lv_message"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_above="@+id/ll_send_1"
        android:scrollbars="none"
        android:divider="@null"
        android:footerDividersEnabled="false">
    </ListView>
    <LinearLayout 
        android:id="@+id/ll_send_1"
        android:layout_height="50dp"
        android:layout_width="fill_parent"
        android:orientation="horizontal"
        android:layout_above="@+id/ll_send_2">
        <EditText 
            android:id="@+id/et_tony_message"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            android:hint="Tony请输入"/>
        <Button 
            android:id="@+id/but_tony_send"
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="send"/>
    </LinearLayout>
    <LinearLayout 
        android:id="@+id/ll_send_2"
        android:layout_height="50dp"
        android:layout_width="fill_parent"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true">
        <EditText 
            android:id="@+id/et_hill_message"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            android:hint="Hill请输入"/>
        <Button 
            android:id="@+id/but_hill_send"
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="send"/>
    </LinearLayout>
</RelativeLayout>
list_message_item_left.xml代码如下



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:orientation="vertical"
	android:background="@drawable/chat_list_item_background"
	android:padding="6dp">

	<LinearLayout
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:orientation="vertical" 
		android:gravity="center_horizontal">
		
		<TextView
			android:id="@+id/tv_sendtime"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:textColor="#8B8B8B"
			android:textSize="12sp"
			android:text="15:03:15"/>
	</LinearLayout>


	<RelativeLayout
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
	
		<ImageView 
			android:id="@+id/iv_userhead" 
			android:layout_width="40dp"
			android:layout_height="40dp"
			android:background="@drawable/head_portrait_1"
			android:focusable="false" 
			android:layout_alignParentLeft="true" 
			android:layout_alignParentTop="true" />
		
		<TextView 
			android:id="@+id/tv_username" 
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:layout_marginLeft="10dp"
			android:layout_marginBottom="5dp"
			android:textColor="#8B8B8B"
			android:layout_toRightOf="@id/iv_userhead"
			android:text="name"/>
		
		<TextView 
			android:id="@+id/tv_chatcontent" 
			android:layout_toRightOf="@id/iv_userhead"
			android:layout_marginLeft="10dp"
			android:layout_marginRight="40dp"
			android:layout_below="@id/tv_username"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:background="@drawable/message_text_background01"/> 
	</RelativeLayout>

</LinearLayout>
list_message_item_right.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:orientation="vertical"
	android:background="@drawable/chat_list_item_background"
	android:padding="6dp">

	<LinearLayout
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:orientation="vertical" 
		android:gravity="center_horizontal">
		
		<TextView
			android:id="@+id/tv_sendtime"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:textColor="#8B8B8B"
			android:textSize="12sp"
			android:text="15:03:15"/>
	</LinearLayout>


	<RelativeLayout
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:layout_marginTop="5dp" >
	
		<ImageView 
			android:id="@+id/iv_userhead" 
			android:layout_width="40dp"
			android:layout_height="40dp"
			android:background="@drawable/head_portrait_2"
			android:focusable="false" 
			android:layout_alignParentRight="true" 
			android:layout_alignParentTop="true" />
		
		<TextView 
			android:id="@+id/tv_username" 
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:layout_marginRight="10dp"
			android:layout_marginBottom="5dp"
			android:textColor="#8B8B8B"
			android:layout_toLeftOf="@id/iv_userhead"
			android:text="name"/>
		
		<TextView 
			android:id="@+id/tv_chatcontent" 
			android:layout_toLeftOf="@id/iv_userhead"
			android:layout_marginRight="10dp"
			android:layout_marginLeft="40dp"
			android:layout_below="@id/tv_username"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			
			android:background="@drawable/message_text_background02"/> 
	</RelativeLayout>

</LinearLayout>
写布局文件的时候会发现有问题,会报错,是因为图片找不到。


需要把图片放在drawable—hdpi文件下

有四张图片是需要复制到drawable-hdpi文件夹下

需要单建一个dawable文件夹里面创建一个名为

chat_list_item_background.xml的文件代码如下


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@color/chat_list_item_background_color"></item>
    <item android:state_pressed="true" android:drawable="@color/chat_list_item_background_color"/>
    <item android:state_selected="true" android:drawable="@color/chat_list_item_background_color"></item>
    <item android:drawable="@color/chat_list_item_background_color" />
</selector>

然后再MainActivity


代码如下


package com.sxt.test;


import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{
	EditText et_tony_message;
	Button but_tony_send;
	
	EditText et_hill_message;
	Button but_hill_send;
	
	ListView lv_message;
	List<Message> list;
	MessageAdapter adapter;
	Handler handler=new Handler();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}

	private void init() {
		et_tony_message=(EditText) findViewById(R.id.et_tony_message);
		but_tony_send=(Button) findViewById(R.id.but_tony_send);
		but_tony_send.setOnClickListener(this);
		
		et_hill_message=(EditText) findViewById(R.id.et_hill_message);
		but_hill_send=(Button) findViewById(R.id.but_hill_send);
		but_hill_send.setOnClickListener(this);
		
		list=new ArrayList<Message>();
		lv_message=(ListView) findViewById(R.id.lv_message);
		adapter=new MessageAdapter();
		lv_message.setAdapter(adapter);
	}


	@Override
	public void onClick(View v) {
		
		if(v==but_tony_send){
			
			if(et_tony_message.getText()==null||et_tony_message.getText().toString().equals("")){
				Toast.makeText(this, "", 0).show();
				return ;
			}
			Message m=new Message();
			m.setFrom_username("Tony");
			m.setCreate_time(System.currentTimeMillis());
			m.setText(et_tony_message.getText().toString());
			sendMessage(m);
			et_tony_message.setText("");
		}
		
		if(v==but_hill_send){
			
			if(et_hill_message.getText()==null||et_hill_message.getText().toString().equals("")){
				Toast.makeText(this, "", 0).show();
				return ;
			}
			Message m=new Message();
			m.setFrom_username("Hill");
			m.setCreate_time(System.currentTimeMillis());
			m.setText(et_hill_message.getText().toString());
			sendMessage(m);
			et_hill_message.setText("");
		}
	}
	private void sendMessage(Message m) {
		list.add(m);
		adapter.notifyDataSetChanged();
//		lv_message.f
		lv_message.setSelection(list.size()+1);
	}
	class MessageAdapter 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) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Message message=list.get(position);
			ViewHolder viewHolder=null;
//		if(convertView==null){
			if("Tony".equalsIgnoreCase(message.getFrom_username())){
				convertView=parent.inflate(MainActivity.this, R.layout.list_message_item_left, null);
			}else{
				convertView=parent.inflate(MainActivity.this, R.layout.list_message_item_right, null);
			}
			viewHolder=new ViewHolder();
			viewHolder.iv_userhead=(ImageView) convertView.findViewById(R.id.iv_userhead);
			viewHolder.tv_chatcontent=(TextView) convertView.findViewById(R.id.tv_chatcontent);
			viewHolder.tv_sendtime=(TextView) convertView.findViewById(R.id.tv_sendtime);
			viewHolder.tv_username=(TextView) convertView.findViewById(R.id.tv_username);
//			convertView.setTag(viewHolder);
//		}else{
//			viewHolder=(ViewHolder) convertView.getTag();
//		}
			
			viewHolder.tv_chatcontent.setText(message.getText());
			viewHolder.tv_sendtime.setText(new SimpleDateFormat("HH:mm:ss").format(new Date(message.getCreate_time())));
			viewHolder.tv_username.setText(message.getFrom_username());
			return convertView;
		}
		class ViewHolder{
			public ImageView iv_userhead;
			public TextView tv_username;
			public TextView tv_chatcontent;
			public TextView tv_sendtime;
		}
	}
}
在创建一个java文件


Messag.java代码如下


package com.sxt.test;

import java.io.Serializable;

public class Message {
	private String from_username;
	private Long create_time;
	private String text;
	public String getFrom_username() {
		return from_username;
	}
	
	public Long getCreate_time() {
		return create_time;
	}
	public void setCreate_time(Long create_time) {
		this.create_time = create_time;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}

	public void setFrom_username(String from_username) {
		this.from_username = from_username;
	}
	
}


然后运行效果如下



北京总部地址:北京市海淀区西三旗桥东建材城西路85号神州科技园B座三层尚学堂 咨询电话:400-009-1906 010-56233821
Copyright 2007-2014 北京尚学堂科技有限公司 京ICP备13018289号-1 京公网安备11010802015183