写了个界面,实现了Android泡泡聊天界面。运行结果如下,点击发送按钮,屏幕就显示Text的内容。我也是在网上的一份源码的基础上更改的,整个泡泡界面的实现要点:(1)主界面其实就是一个List View(2)文字显示界面其实就使用了android:background="@drawable/incoming"这个东西。背景图片的格式是xxx.9.png,专门用来缩放的,不然显示效果非常差。(3)自定义了一个adapter,当然是继承android.widget.BaseAdapter,重写了getView的方法。整个工程分布如下:主activity: ChatActivity如下:
- package com.tencent;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListView;
-
- import java.util.ArrayList;
- import java.util.Calendar;
-
- public class ChatActivity extends Activity {
- private static final String TAG = ChatActivity.class.getSimpleName();;
-
- private ListView talkView;
-
- private Button messageButton;
-
- private EditText messageText;
-
- // private ChatMsgViewAdapter myAdapter;
-
- private ArrayList<ChatMsgEntity> list = new ArrayList<ChatMsgEntity>();
-
- public void onCreate(Bundle savedInstanceState) {
- Log.v(TAG, "onCreate >>>>>>");
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- talkView = (ListView) findViewById(R.id.list);
- messageButton = (Button) findViewById(R.id.MessageButton);
- messageText = (EditText) findViewById(R.id.MessageText);
- OnClickListener messageButtonListener = new OnClickListener() {
-
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- Log.v(TAG, "onclick >>>>>>>>");
- String name = getName();
- String date = getDate();
- String msgText = getText();
- int RId = R.layout.list_say_he_item;
-
- ChatMsgEntity newMessage = new ChatMsgEntity(name, date, msgText, RId);
- list.add(newMessage);
- // list.add(d0);
- talkView.setAdapter(new ChatMsgViewAdapter(ChatActivity.this, list));
- messageText.setText("");
- // myAdapter.notifyDataSetChanged();
- }
-
- };
- messageButton.setOnClickListener(messageButtonListener);
- }
-
- // shuold be redefine in the future
- private String getName() {
- return getResources().getString(R.string.myDisplayName);
- }
-
- // shuold be redefine in the future
- private String getDate() {
- Calendar c = Calendar.getInstance();
- String date = String.valueOf(c.get(Calendar.YEAR)) + "-"
- + String.valueOf(c.get(Calendar.MONTH)) + "-" + c.get(c.get(Calendar.DAY_OF_MONTH));
- return date;
- }
-
- // shuold be redefine in the future
- private String getText() {
- return messageText.getText().toString();
- }
-
- public void onDestroy() {
- Log.v(TAG, "onDestroy>>>>>>");
- // list = null;
- super.onDestroy();
- }
- }