Excerpt from the first line of code--Guo Lin
Since you are writing a chat interface, you must have received the message and sent the message. The message_left.9.png we made in the previous section can be used as a background for receiving messages, so there is no doubt that you will need to create a message_right.9.png as a background for sending messages.
Once the pictures are available, you can start coding, first writing the main interface, and modifying the code in Activity_main.xml as follows:
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "#d8e0e8"android:orientation= "vertical" > <ListViewAndroid:id= "@+id/msg_list_view"Android:layout_width= "Match_parent"Android:layout_height= "0DP"Android:layout_weight= "1"Android:divider= "#0000" > </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>
Here, a ListView is placed in the main interface to display the message content of the chat, a edittext is placed for the input message, and a button is placed to send the message. A Android:divider property is used in the ListView that specifies the color of the ListView divider, where #0000 indicates that the divider is set to a transparent color. All the other properties that we have learned before, believe that you should understand that it should be effortless.
We then define the entity class for the message, create a new MSG, and the code looks like this:
Public classMSG { Public Static Final inttype_received = 0; Public Static Final intType_sent = 1; PrivateString content; Private inttype; PublicMSG (String content,inttype) { This. Content =content; This. Type =type; } PublicString getcontent () {returncontent; } Public intGetType () {returntype; }}
There are only two fields in the Msg class, content represents the contents of the message, and type represents the message. Where the message type has two values to choose from, type_received indicates that this is a received message, and Type_sent indicates that this is a message sent.
Next, write the layout of the ListView subkey, create a new msg_item.xml, and the code looks like this:
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"android:padding= "10DP" > <LinearLayoutAndroid:id= "@+id/left_layout"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Left"Android:background= "@drawable/message_left" > <TextViewAndroid:id= "@+id/left_msg"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"Android:layout_margin= "10DP"Android:textcolor= "#fff" /> </LinearLayout> <LinearLayoutAndroid:id= "@+id/right_layout"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Right"Android:background= "@drawable/message_right" > <TextViewAndroid:id= "@+id/right_msg"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"Android:layout_margin= "10DP" /> </LinearLayout></LinearLayout>
Here we let the received message be left-aligned, the messages emitted are aligned to the right, and the message_left.9.png and message_right.9.png are used respectively as the background map. You may have some doubts about how you can keep the messages you receive and the messages you send in the same layout. Don't worry, remember the visible properties we learned in the previous section, as long as you later decide what kind of message to hide and display in your code based on the type of message.
Next you need to create the adapter class for the ListView, let it inherit from Arrayadapter, and specify the generic as the MSG class. To create a new class Msgadapter, the code looks like this:
Public classMsgadapterextendsArrayadapter<msg> { Private intresourceId; PublicMsgadapter (Context context,intTextviewresourceid, list<msg>objects) { Super(Context, Textviewresourceid, objects); ResourceId=Textviewresourceid; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {msg msg=GetItem (position); View view; Viewholder Viewholder; if(Convertview = =NULL) {View= Layoutinflater.from (GetContext ()). Inflate (ResourceId,NULL); Viewholder=NewViewholder (); Viewholder.leftlayout=(linearlayout) view. Findviewbyid (R.id.left_layout); Viewholder.rightlayout=(linearlayout) view. Findviewbyid (R.id.right_layout); Viewholder.leftmsg=(TextView) View.findviewbyid (r.id.left_msg); Viewholder.rightmsg=(TextView) View.findviewbyid (r.id.right_msg); View.settag (Viewholder); } Else{View=Convertview; Viewholder=(Viewholder) View.gettag (); } if(Msg.gettype () = =msg.type_received) { //If the message is received, the left message layout is displayed, and the message layout on the right is hiddenviewHolder.leftLayout.setVisibility (view.visible); ViewHolder.rightLayout.setVisibility (View.gone); ViewHolder.leftMsg.setText (Msg.getcontent ()); } Else if(Msg.gettype () = =msg.type_sent) { //If the message is emitted, the message layout on the right is displayed, and the left message layout is hiddenviewHolder.rightLayout.setVisibility (view.visible); ViewHolder.leftLayout.setVisibility (View.gone); ViewHolder.rightMsg.setText (Msg.getcontent ()); } returnview; } classViewholder {linearlayout leftlayout; LinearLayout rightlayout; TextView leftmsg; TextView rightmsg; }}
The above code you should be very familiar with, and we learn the ListView that section of the code is basically the same, but in the GetView () method added to the message type of judgment. If this message is received, the message layout on the left is displayed, and if the message is emitted, the message layout on the right is displayed.
Finally, modify the code in mainactivity to initialize some data for the ListView and add an event response to the Send button, as shown in the following code:
Public classMainactivityextendsActivity {PrivateListView Msglistview; PrivateEditText Inputtext; PrivateButton Send; PrivateMsgadapter Adapter; PrivateList<msg> msglist =NewArraylist<msg>(); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_main); Initmsgs (); //Initializing message dataadapter =NewMsgadapter (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 (NewOnclicklistener () {@Override Public voidOnClick (View v) {String content=Inputtext.gettext (). toString (); if(!"". Equals (content)) {msg msg=NewMsg (content, msg.type_sent); Msglist.add (msg); Adapter.notifydatasetchanged (); //refreshes the display in the ListView when new messages are availableMsglistview.setselection (Msglist.size ());//position the ListView to the last rowInputtext.settext ("");//empty the contents of the input box } } }); } Private voidinitmsgs () {MSG MSG1=NewMSG ("Hello guy.", msg.type_received); Msglist.add (MSG1); MSG MSG2=NewMSG ("Hello." Who's that? ", msg.type_sent); Msglist.add (MSG2); MSG MSG3=NewMSG ("This is Tom.") Nice talking. ", msg.type_received); Msglist.add (MSG3); }}
In the Initmsgs () method we first initialized a few data for display in the ListView. It then gets the contents of the EditText in the Click event of the Send button, creates a new MSG object if the content is not empty, and adds it to the Msglist list. The adapter's notifydatasetchanged () method is then called, and the data for the notification list changes so that a new message can be displayed in the ListView. The ListView SetSelection () method is then called to position the displayed data to the last row to ensure that a last message is visible. Finally, call EditText's SetText () method to empty the input.
So all the work is done, finally can test our results, after running the program you will see a very beautiful chat interface, and you can enter and send messages.
After this example of the actual combat, I believe that not only deepened your understanding of the UI knowledge in this chapter, but also gives you the flexibility to use this knowledge to design a good interface ideas.
android# writing a chat interface