类似iphone的短消息效果

类似iphone的短消息效果

 

 1 package com.ql.app;  
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import android.app.Activity;
7 import android.content.Intent;
8 import android.os.Bundle;
9 import android.view.View;
10 import android.widget.Button;
11 import android.widget.EditText;
12 import android.widget.ImageView;
13 import android.widget.ListView;
14
15 public class ChatActivity extends Activity {
16
17 private ChattingAdapter adapter;
18 private List<ChatMessage> messages = new ArrayList<ChatMessage>();
19 private ListView listView;
20 private Button btn_send;
21 private EditText textEditor;
22 private ImageView btn_insert;
23
24 /** Called when the activity is first created. */
25 @Override
26 public void onCreate(Bundle savedInstanceState) {
27 super.onCreate(savedInstanceState);
28 setContentView(R.layout.main);
29
30 messages.add(new ChatMessage(ChatMessage.MESSAGE_FROM, "hello,android."));
31 messages.add(new ChatMessage(ChatMessage.MESSAGE_TO, "hello,java."));
32 messages.add(new ChatMessage(ChatMessage.MESSAGE_FROM, "请教高手,怎么可以给gridview设置边框啊,现在这个显示效果实在太难看了。"));
33 messages.add(new ChatMessage(ChatMessage.MESSAGE_TO, "那就加点背景,加点属性什么的不就行了吗"));
34 messages.add(new ChatMessage(ChatMessage.MESSAGE_FROM, "在主题中,将gridview的cssclass设置为gridview"));
35 messages.add(new ChatMessage(ChatMessage.MESSAGE_TO, "谢谢"));
36 listView=(ListView)findViewById(R.id.listView);
37 adapter = new ChattingAdapter(this, messages);
38 listView.setAdapter(adapter);
39
40 btn_send = (Button) findViewById(R.id.btn_send);
41 textEditor = (EditText) findViewById(R.id.text_editor);
42
43 btn_insert=(ImageView) findViewById(R.id.btn_insert);
44 btn_send.setOnClickListener(listener);
45 btn_insert.setOnClickListener(listener);
46 }
47
48
49 private View.OnClickListener listener = new View.OnClickListener() {
50
51 @Override
52 public void onClick(View v) {
53 switch (v.getId()) {
54 case R.id.btn_send:
55 String str = textEditor.getText().toString();
56 String sendStr=str.trim();
57 if(!sendStr.equals("")){
58 sendMessage(sendStr);
59 }
60 textEditor.setText("");
61 break;
62 case R.id.btn_insert:
63 Intent i = new Intent();
64 i.setType("image/*");
65 i.setAction(Intent.ACTION_GET_CONTENT);
66 startActivityForResult(i, Activity.DEFAULT_KEYS_SHORTCUT);
67
68 // Intent it = new Intent("android.media.action.IMAGE_CAPTURE");
69 // startActivityForResult(it, Activity.DEFAULT_KEYS_DIALER);
70 break;
71 default:
72 break;
73 }
74 }
75
76 // 模拟发送消息
77 private void sendMessage(String sendStr) {
78 messages.add(new ChatMessage(ChatMessage.MESSAGE_TO, sendStr));
79 adapter.notifyDataSetChanged();
80 }
81
82 };
83
84
85
86 }

 

 1 <?xml version="1.0" encoding="utf-8"?>  
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 android:focusable="false"
7 android:focusableInTouchMode="false"
8 android:background="@drawable/nav_page"
9 android:gravity="center_horizontal"
10 >
11 <ListView
12 android:id="@+id/listView"
13 android:background="@null"
14 android:scrollbars="vertical"
15 android:layout_width="fill_parent"
16 android:layout_height="wrap_content"
17 android:listSelector="@null"
18 android:transcriptMode="alwaysScroll"
19 android:cacheColorHint="#00000000"
20 android:divider="@null"
21 android:layout_weight="1.0"
22 />
23 <LinearLayout
24 android:orientation="horizontal"
25 android:layout_width="fill_parent"
26 android:layout_height="wrap_content"
27 android:background="@drawable/txt_msg_bg"
28 android:paddingRight="7.0dip"
29 >
30 <ImageView
31 android:id="@+id/btn_insert"
32 android:layout_gravity="center_vertical"
33 android:paddingLeft="15.0dip"
34 android:paddingTop="5.0dip"
35 android:paddingRight="7.0dip"
36 android:paddingBottom="5.0dip"
37 android:layout_width="wrap_content"
38 android:layout_height="wrap_content"
39 android:src="@drawable/sms_insert"
40 />
41 <EditText
42 android:id="@+id/text_editor"
43 android:layout_width="0.0dip"
44 android:layout_height="wrap_content"
45 android:layout_gravity="center_vertical"
46 android:background="@drawable/sms_embeded_text_editor_bg"
47 android:focusable="true"
48 android:nextFocusRight="@+id/send_button"
49 android:layout_marginLeft="7.0dip"
50 android:layout_marginTop="5.0dip"
51 android:layout_marginRight="7.0dip"
52 android:layout_marginBottom="5.0dip"
53 android:minHeight="34.0dip"
54 android:hint="输入消息"
55 android:maxLines="8"
56 android:maxLength="2000"
57 android:capitalize="sentences"
58 android:autoText="true"
59 android:layout_weight="1.0"
60 android:inputType="textCapSentences|textAutoCorrect|textMultiLine|textShortMessage"
61 android:imeOptions="actionSend|flagNoEnterAction"
62 />
63 <Button
64 android:id="@+id/btn_send"
65 android:layout_width="wrap_content"
66 android:layout_height="wrap_content"
67 android:gravity="center"
68 android:layout_gravity="center_vertical"
69 android:background="@drawable/sms_send_button_bg"
70 android:paddingLeft="11.0dip"
71 android:paddingRight="11.0dip"
72 android:nextFocusLeft="@id/text_editor"
73 />
74 </LinearLayout>
75 </LinearLayout>


原文连接:http://gundumw100.iteye.com/blog/1056517

你可能感兴趣的:(iPhone)