当前位置:操作系统 > 安卓/Android >>

android学习笔记30--------------聊天式对话效果

这几天看了些聊天式对话效果,自己修改了一番。

 \

 

public class ChatActivity extends Activity 

    private ListView talkView; 
    private Button messageButton; 
    private EditText messageText; 
    private ArrayList<ChatMessage> list = new ArrayList<ChatMessage>(); 
 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        init(); 
    } 
 
    private void init() 
    { 
        talkView = (ListView) findViewById(R.id.list); 
        messageButton = (Button) findViewById(R.id.MessageButton); 
        messageText = (EditText) findViewById(R.id.MessageText); 
        messageButton.setOnClickListener(new OnClickListener() 
        { 
 
            @Override 
            public void onClick(View v) 
            { 
                String date = getDate(); 
                String msgText = getText(); 
                int RIdA = R.layout.chatting_item_from; 
                ChatMessage newMsg = new ChatMessage( date, msgText, RIdA); 
                 
                list.add(newMsg); 
                int RIdB = R.layout.chatting_item_to; 
 
                ChatMessage backMsg = new ChatMessage(date, 
                        "自动回复(for test!)", RIdB); 
                list.add(backMsg); 
                talkView 
                        .setAdapter(new ChattingAdapter(ChatActivity.this, list)); 
                messageText.setText(""); 
            } 
        }); 
    } 
 
    private String getDate() 
    { 
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm"); 
        Date d = new Date(); 
        return sdf.format(d); 
    } 
 
    private String getText() 
    { 
        return messageText.getText().toString(); 
    } 
 

public class ChatActivity extends Activity
{
 private ListView talkView;
 private Button messageButton;
 private EditText messageText;
 private ArrayList<ChatMessage> list = new ArrayList<ChatMessage>();

 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  init();
 }

 private void init()
 {
  talkView = (ListView) findViewById(R.id.list);
  messageButton = (Button) findViewById(R.id.MessageButton);
  messageText = (EditText) findViewById(R.id.MessageText);
  messageButton.setOnClickListener(new OnClickListener()
  {

   @Override
   public void onClick(View v)
   {
    String date = getDate();
    String msgText = getText();
    int RIdA = R.layout.chatting_item_from;
    ChatMessage newMsg = new ChatMessage( date, msgText, RIdA);
    
    list.add(newMsg);
    int RIdB = R.layout.chatting_item_to;

    ChatMessage backMsg = new ChatMessage(date,
      "自动回复(for test!)", RIdB);
    list.add(backMsg);
    talkView
      .setAdapter(new ChattingAdapter(ChatActivity.this, list));
    messageText.setText("");
   }
  });
 }

 private String getDate()
 {
  SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
  Date d = new Date();
  return sdf.format(d);
 }

 private String getText()
 {
  return messageText.getText().toString();
 }

}
[java]
public class ChatMessage 

 
    private String date; 
    private String text; 
    private int layoutID; 
 
    public ChatMessage(String date, String text, int id) 
    { 
        this.date = date; 
        this.text = text; 
        this.layoutID = id; 
    } 
 
    public String getDate() 
    { 
        return date; 
    } 
 
    public void setDate(String date) 
    { 
        this.date = date; 
    } 
 
    public String getText() 
    { 
        return text; <

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,