Android(安卓)一个简单的聊天界面的实现(eclipse实现)
這幾天剛剛學習一下安卓的編程,嘗試制作了一個簡單的聊天界面(還沒有實現網絡等后續功能)軟件界面如圖。(使用eclipse實現)
當輸入一些內容后,聊天界面可以下拉顯示更多的聊天信息,如下圖
首先對這個聊天軟件的界面進行一個總結,要能夠實現聊天信息的下拉功能,需要使用ListView空間,文字輸入界面需要使用EditView,按鈕的實驗需要用到Button,關于按鈕,需要為按鈕添加相關的事件,關于ListView,需要為其添加適配器,這個后面一步步的來進行。
首先,是修改activity_main.xml 文件,使其呈現出一個聊天界面的框架。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><ListViewandroid:id="@+id/msg_list_view"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" ></ListView><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><EditTextandroid:id="@+id/input_text"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:hint="Type somthing here"android:maxLines="2" /><Buttonandroid:id="@+id/send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Send" /></LinearLayout> </LinearLayout>Button空間中的text=“Send”語句,編譯器提示警告,一般程序員都知道,警告的東西往往可以忽略。其實這樣寫也不算錯誤,但是不是非常的規范,一般情況下,都是定義一個字符串的名字,這里直接飲用了字符串的名字,然后字符串的名字與字符串的對應關系在string.xml文件中存儲,這才是規范的使用方式。具體這個細節大家有興趣的可以再去查一下相關資料。這里的警告我們就忽視他就好了。
可以看到通過上述的xml代碼,我們就已經實現了這個聊天界面的外觀,其中layout_weight這個屬性非常重要,這個屬性表示在屏幕空間的剩余部分,按照layout_weight所表示的比例來占據空間的大小(不懂的查一下,網上有些資料說的很詳細了),然后考慮實現它的一些簡單的功能。
我們定義一個消息類Msg,其代碼如下
public class Msg { public static final int TYPE_RECEIVED = 0; public static final int TYPE_SENT = 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; } }Msg類中只有兩個字段,content表示消息的內容,type 表示消息的類型。其中消息類型有兩個值可選,TYPE_RECEIVED 表示這是一條收到的消息,TYPE_SENT 表示這是一條發出的消息。
接著來編寫 ListView子項的布局,新建 msg_item.xml,代碼如下所示:
這里把發送消息和接收消息的布局全部都寫在了一個文件里,稍后的使用中,我們通過調整其可見性,就可以分別實現發送和接收消息的布局,例如發送的消息,就把接收消息的布局設置為不可見即可。
接下來需要創建 ListView的適配器類,讓它繼承自 ArrayAdapter,并將泛型指定為 Msg類。新建類 MsgAdapter,代碼如下
最后修改 MainActivity中的代碼,來為 ListView初始化一些數據,并給發送按鈕加入事件響應,代碼如下
public class MainActivity extends Activity {private ListView msgListView;private EditText inputText;private Button send;private MsgAdapter adapter;private List<Msg> msgList = new ArrayList<Msg>(); @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);initMsgs(); // 初始化消息數據adapter = new MsgAdapter(MainActivity.this, R.layout.msg_item, msgList);inputText = (EditText) findViewById(R.id.input_text);send = (Button) findViewById(R.id.send);msgListView = (ListView) findViewById(R.id.msg_list_view);msgListView.setAdapter(adapter);send.setOnClickListener(new OnClickListener() { @Overridepublic void onClick(View v) {String content = inputText.getText().toString();if (!"".equals(content)) {Msg msg = new Msg(content, Msg.TYPE_SENT);msgList.add(msg);adapter.notifyDataSetChanged(); // 當有新消息時,刷新ListView中的顯示msgListView.setSelection(msgList.size()); // 將ListView定位到最后一行inputText.setText(""); // 清空輸入框中的內容}}}); } private void initMsgs() {Msg msg1 = new Msg("Hello guy.", Msg.TYPE_RECEIVED);msgList.add(msg1);Msg msg2 = new Msg("Hello. Who is that?", Msg.TYPE_SENT);msgList.add(msg2);Msg msg3 = new Msg("This is Tom. Nice talking to you. ", Msg.TYPE_RECEIVED);msgList.add(msg3);} }在這個文件中,我們初始化出了一些消息,并且為按鈕添加的事件的響應。
至此,這個聊天界面的小程序已經完成,已經可以導入手機并在手機上運行了。
2015年9月29日 ? 西安交通大學
總結
以上是生活随笔為你收集整理的Android(安卓)一个简单的聊天界面的实现(eclipse实现)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用SIFt特征点和RANSAC方法进行
- 下一篇: git与github区别与简介