Android Analog chat interface---adapted the first line of code

Source: Internet
Author: User
<span id="Label3"></p><p><p>Main interface</p></p><p><p></p></p><p><?xml version= "1.0" encoding= "utf-8"?><br><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"<br>Android:layout_width= "match_parent"<br>android:layout_height= "match_parent"<br>Android:background= "#d8e0e8"<br>android:orientation= "vertical" ><br> <br><listview<br>Android:id= "@+id/msg_list_view"<br>Android:layout_width= "match_parent"<br>android:layout_height= "0dp"<br>android:layout_weight= "1"<br>android:divider= "#000" ></ListView><br> <br><linearlayout<br>Android:layout_width= "match_parent"<br>android:layout_height= "wrap_content"<br>><br> <br><edittext<br>Android:id= "@+id/input_text"<br>Android:layout_width= "0dip"<br>android:layout_weight= "1"<br>android:layout_height= "wrap_content"<br>android:hint= "please Enter the chat content"/><br> <br><button<br>Android:id= "@+id/send"<br>Android:layout_width= "wrap_content"<br>android:layout_height= "wrap_content"<br>android:text= "send" ></Button><br> <br> <br> <br></LinearLayout><br> </p><p><p></LinearLayout></p></p><p><p></p></p><p><p>Message Item</p></p><p><?xml version= "1.0" encoding= "utf-8"?><br><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"<br>Android:layout_width= "match_parent"<br>android:layout_height= "match_parent"<br>android:orientation= "vertical"<br>android:padding= "10dp" ><br> <br><linearlayout<br>Android:id= "@+id/left_layout"<br>Android:layout_width= "wrap_content"<br>android:layout_height= "wrap_content"<br>android:layout_gravity= "left"<br>android:background= "@drawable/message_left" ><br> <br><textview<br>Android:id= "@+id/left_msg"<br>Android:layout_width= "wrap_content"<br>android:layout_height= "wrap_content"<br>android:layout_gravity= "center"<br>android:layout_margin= "10dip"<br>Android:textcolor= "#fff"/><br></LinearLayout><br> <br> <br><linearlayout<br>Android:id= "@+id/right_layout"<br>Android:layout_width= "wrap_content"<br>android:layout_height= "wrap_content"<br>android:layout_gravity= "right"<br>android:background= "@drawable/message_right" ><br> <br><textview<br>Android:id= "@+id/right_msg"<br>Android:layout_width= "wrap_content"<br>android:layout_height= "wrap_content"<br>android:layout_gravity= "center"<br>android:layout_margin= "10dip"<br>Android:textcolor= "#fff"/><br></LinearLayout><br> <br> <br> </p><p><p></LinearLayout></p></p><p><p></p></p><p><p>Message class</p></p><p><p></p></p><p><p>Package com.example.thingking_in_android;</p></p><p><p>Message class<br>public class MSG {<br>public static Boolean flag=true;<br><br>public static final int type_received=0;<br><br>public static final int type_sent=1;<br><br>Private String content;<br>private int type;<br>Public String getcontent () {<br>Return content;<br>}<br>public void SetContent (String Content) {<br>This.content = content;<br>}<br>public int GetType () {<br>Return type;<br>}<br>public void SetType (int Type) {<br>This.type = type;<br>}<br>Public MSG (String content, int Type) {<br>Super ();<br>This.content = content;<br>This.type = type;<br>}<br><br><br><br>}</p></p><p><p></p></p><p><p>Main class</p></p><p><p></p></p><p><p>Package com.example.thingking_in_android;</p></p><p><p>Import java.util.ArrayList;<br>Import java.util.List;<br>Import java.util.zip.Inflater;</p></p><p><p>Import Com.example.thingking_in_android. r.layout;</p></p><p><p>Import Android. r.anim;<br>Import android.app.Activity;<br>Import android.content.Intent;<br>Import android.os.Bundle;<br>Import android.view.LayoutInflater;<br>Import android.view.Menu;<br>Import android.view.View;<br>Import android.view.Window;<br>Import android.view.View.OnClickListener;<br>Import android.view.ViewGroup;<br>Import android.widget.AdapterView;<br>Import android.widget.AdapterView.OnItemClickListener;<br>Import android.widget.ArrayAdapter;<br>Import android.widget.BaseAdapter;<br>Import android.widget.Button;<br>Import android.widget.EditText;<br>Import android.widget.ImageView;<br>Import android.widget.LinearLayout;<br>Import android.widget.ListView;<br>Import android.widget.TextView;<br>Import android.widget.Toast;</p></p><p><p>/**<br>* Analog Chat<br>* @author Yellow Two dog reference the first line of code adaptation<br>*<br>*/</p></p><p><br>public class Helloactivity extends Activity {<br>Chat List View<br>Private ListView msglistview;<br><br>Input Information box<br>Private EditText inputtext;<br><br>Send button<br>Private Button send;<br><br>list<msg>list;<br><br>Myadapter adapter;<br><br><br><br>@Override<br>protected void OnCreate (Bundle Savedinstancestate) {<br> <br>System.out.println ("begin");<br>Super.oncreate (savedinstancestate);<br>Do not display the title bar<br>Requestwindowfeature (window.feature_no_title);<br><br>Loading layout files<br>This.setcontentview (r.layout.activity_main);<br><br>Setting up data<br>List=new ArrayList ();<br>Msg msg1=new msg ("Hello I ' am jack!", msg.type_received);<br>MSG msg2=new msg ("Hello is ok?", msg.type_sent);<br>Msg msg3=new msg ("Yes I am very ok!", msg.type_received);<br>MSG msg4=new msg ("that's all right!", msg.type_sent);<br>List.add (msg1);<br>List.add (msg2);<br>List.add (msg3);<br>List.add (msg4);<br><br><br>Get the corresponding View component<br>inputtext= (EditText) Findviewbyid (r.id.input_text);<br>send= (Button) Findviewbyid (r.id.send);<br>msglistview= (ListView) Findviewbyid (r.id.msg_list_view);<br><br>Adapter=new Myadapter ();<br>System.out.println (adapter);<br>System.out.println (list);<br>Msglistview.setadapter (adapter);<br>Add an event for the Send button<br>Send.setonclicklistener (new Onclicklistener () {<br><br>@Override<br>public void OnClick (View V) {<br>Get user input data<br>String content=inputtext.gettext (). toString ();<br>Determine if the data is empty<br>If (! "". equals (content))<br>{<br>Simulate a one-off situation<br><br>Msg msg;<br>If (msg.flag)<br>{<br>Msg=new MSG (content,msg.type_sent);<br><br>}<br>Else<br>{<br>Msg=new MSG (content,msg.type_received);<br>}<br><br>msg.flag=! msg.flag;<br>List.add (msg);<br><br>Adapter.notifydatasetchanged (); Refreshes the display of the ListView when new messages are available<br><br>Inputtext.settext ("");<br>}<br><br>}<br>});<br><br><br><br>}<br><br><br>Class Myadapter extends Baseadapter<br>{</p><p><p>@Override<br>public int GetCount () {<br><br>return list.size ();<br>}</p></p><p><p>@Override<br>Public Object GetItem (int Position) {<br>return List.get (position);<br>}</p></p><p><p>@Override<br>Public long Getitemid (int Position) {<br>TODO auto-generated Method Stub<br>Return 0;<br>}</p></p><p>@Override<br>Public View getView (int position, View convertview, viewgroup Parent) {<br> <br><br>System.out.println ("load = = =");<br>Loading layouts and setting up data<br><br><br>MSG Msg=list.get (position);<br>View view;<br>Viewholder viewholder;<br>The layout reloads so it needs buffering<br>If (convertview==null)<br>{<br>View=layoutinflater.from (helloactivity.this). Inflate (r.layout.msg_item,null);<br>To store a view in an object<br>Viewholder=new Viewholder ();<br> <br>viewholder.leftlayout= (linearlayout) View.findviewbyid (r.id.left_layout);<br>viewholder.rightlayout= (linearlayout) View.findviewbyid (r.id.right_layout);<br> <br>viewholder.leftmsg= (TextView) View.findviewbyid (r.id.left_msg);<br>viewholder.rightmsg= (TextView) View.findviewbyid (r.id.right_msg);<br><br>Views can store objects<br>View.settag (viewholder);<br><br>}<br>Else<br>{<br>view=convertview;<br>viewholder= (viewholder) View.gettag ();<br>}<br> <br><br> <br>Set up resources<br>If (msg.gettype () ==msg.type_received)<br>{<br>ViewHolder.leftLayout.setVisibility (view.visible);<br>ViewHolder.rightLayout.setVisibility (view.gone);<br>ViewHolder.leftMsg.setText (msg.getcontent ());<br>}<br><br>else if (msg.gettype () ==msg.type_sent)<br>{<br>ViewHolder.rightLayout.setVisibility (view.visible);<br>ViewHolder.leftLayout.setVisibility (view.gone);<br>ViewHolder.rightMsg.setText (msg.getcontent ());<br>}<br><br>Return view;<br><br><br>}<br><br>}<br><br><br>}</p><p><p>Android Analog chat interface---adapted the first line of code</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.