At present, a variety of app community or users to bask in the photos, said the place, have provided a comment function, in order to better learn, the function to achieve a bit, made a small demo.
First recommend a practical plug-in layoutcreater, can help developers automatically generate layout code, specific use can go to Gihub to see:
GitHub Address: Https://github.com/boredream/BorePlugin
1, a new Android project, write mainactivity layout activity_main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent "Android:layout_height=" match_parent "android:background=" @color/grey "> <listview android:id=" @+id/comment_ List "android:layout_width=" Match_parent "android:layout_height=" match_parent "android:layout_margintop=" 10DP "and roid:layout_marginbottom= "50DP"/> <linearlayout android:id= "@+id/rl_enroll" android:layout_width= "Match_" Parent "android:layout_height=" 50DP "android:orientation=" Horizontal "android:layout_alignparentbottom=" true "Andr oid:background= "@color/white" > <imageview android:id= "@+id/comment" android:layout_width= "32DP" Android: layout_height= "32DP" android:src= "@drawable/comment" android:layout_weight= "1" android:layout_gravity= "center"/&
Gt <imageview android:id= "@+id/chat" android:layout_width= "23DP" android:layout_height= "23DP" android:src= "@dr Awable/chat "Android:layout_weight= "1" android:layout_gravity= "center"/> </LinearLayout> <relativelayout android:id= "@+id/rl_comme" NT "android:layout_width=" Match_parent "android:layout_height=" 50DP "android:background=" @color/white "Android:vis Ibility= "Gone" android:layout_alignparentbottom= "true" > <view android:layout_width= "match_parent" Android : layout_height= "1DP" android:background= "@color/grey"/> <textview android:id= "@+id/hide_down" android:l Ayout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/hide_down" android:textSize = "13SP" android:textcolor= "@color/txtgrey" android:drawablebottom= "@drawable/hide_dowm" Android:layout_alignparen Tleft= "true" android:layout_centervertical= "true" android:layout_marginleft= "10DP"/> <view android:layout _width= "1DP" android:layout_height= "match_parent" android:background= "@color/grey" android:layout_torightof= "@id Hide_down "Android:layout_marginleft= "10DP"/> <edittext android:id= "@+id/comment_content" android:hint= "@string/comment_content" Android:textsize= "15SP" android:singleline= "true" android:layout_width= "240DP" android:layout_height= "Match_p"
Arent "android:background=" @null "android:layout_torightof=" @id/hide_down "android:layout_marginleft=" 20DP "/> <button android:id= "@+id/comment_send" android:layout_width= "50DP" android:layout_height= "35DP" Android: Layout_margin= "5DP" android:text= "@string/send" android:textsize= "13sp" android:textcolor= "@color/white" Andro id:background= "@color/maincolor" android:layout_alignparentright= "true" android:layout_marginright= "10DP" Android
: layout_marginleft= "15DP"/> </RelativeLayout> </RelativeLayout>
2. Create comment Content entity class, content adapter, content item layout
1) Content entity class Comment
public class Comment {
string name;//reviewer
String content;//comments Content Public
Comment () {
} public
Comment (string name, string content) {
this.name = name;
this.content = content;
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
Public String getcontent () {return
content;
}
public void SetContent (String content) {
this.content = content;
}
}
2) Content Adapter Adaptercomment
public class Adaptercomment extends Baseadapter;
List<comment> data;
Public adaptercomment (context C, list<comment> data) {this.context = C;
This.data = data;
@Override public int GetCount () {return data.size ();
@Override public Object getitem (int i) {return data.get (i);
@Override public long Getitemid (int i) {return i;
@Override public View GetView (int i, View convertview, ViewGroup viewgroup) {Viewholder holder;
Reusing Convertview if (Convertview = = null) {holder = new Viewholder ();
Convertview = Layoutinflater.from (context). Inflate (r.layout.item_comment, NULL);
Holder.comment_name = (TextView) Convertview.findviewbyid (r.id.comment_name);
Holder.comment_content = (TextView) Convertview.findviewbyid (r.id.comment_content);
Convertview.settag (holder);
}else{holder = (viewholder) convertview.gettag ();
}//Adapter Data Holder.comment_name.setText (Data.get (i) getName ()); Holder.comment_conteNt.settext (Data.get (i). GetContent ());
return convertview;
/** * Add a comment, refresh the list * @param comment */public void addcomment (comment comment) {data.add (comment);
Notifydatasetchanged ();
/** * Static class, Easy GC recovery/public static class viewholder{TextView comment_name;
TextView comment_content;
}
}
3 The content of the item layout item_comment.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:orientation= "Horizontal "
android:layout_width=" match_parent "
android:layout_height=" match_parent ">
<textview
Android:id= "@+id/comment_name"
android:layout_width= "wrap_content"
android:layout_height= "Wrap_content" "
android:textcolor=" @color/maincolor "
android:textsize=" 15sp "
android:layout_marginleft=" 15DP "
android:layout_marginright= "3DP"/>
<textview
android:id= "@+id/comment_content"
Android:layout_width= "Wrap_content"
android:layout_height= "wrap_content"
android:textcolor= "@color Coloraccent "
android:textsize=" 15sp "/>
</LinearLayout>
3, in mainactivity selected layout, then the menu bar click Code-> Layoutcreater, determine the layout code to generate, click Confirm Complete
The next refinement, the specific implementation I have in the code to do a comment, not specifically said
public class Mainactivity extends activity implements View.onclicklistener {private ImageView comment;
Private TextView Hide_down;
Private EditText comment_content;
Private Button comment_send;
Private LinearLayout Rl_enroll;
Private Relativelayout rl_comment;
Private ListView comment_list;
Private Adaptercomment adaptercomment;
Private list<comment> data;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Initview ();
private void Initview () {//initialization comment List comment_list = (ListView) Findviewbyid (r.id.comment_list);
Initialize data = new arraylist<> ();
Initialize Adapter adaptercomment = new Adaptercomment (Getapplicationcontext (), data);
Set Adapter Comment_list.setadapter (adaptercomment) for the comment list;
Comment = (imageview) Findviewbyid (r.id.comment);
Hide_down = (TextView) Findviewbyid (R.id.hide_down); Comment_content = (edittext) Findviewbyid (R.id.comment_contENT);
Comment_send = (Button) Findviewbyid (r.id.comment_send);
Rl_enroll = (linearlayout) Findviewbyid (R.id.rl_enroll);
Rl_comment = (relativelayout) Findviewbyid (r.id.rl_comment);
Setlistener ();
/** * Set listening/public void Setlistener () {Comment.setonclicklistener (this);
Hide_down.setonclicklistener (this);
Comment_send.setonclicklistener (this); @Override public void OnClick (View v) {switch (V.getid ()) {case r.id.comment://Pop-up Input Method Inputmethodmana
Ger Imm = (Inputmethodmanager) getapplicationcontext (). Getsystemservice (Context.input_method_service);
Imm.togglesoftinput (0, inputmethodmanager.hide_not_always);
Show Comment Box rl_enroll.setvisibility (view.gone);
Rl_comment.setvisibility (view.visible);
Break
Case R.id.hide_down://Hide Comment box rl_enroll.setvisibility (view.visible);
Rl_comment.setvisibility (View.gone); Hide the input method, and then temporarily store the contents of the current input box to facilitate the next use of Inputmethodmanager im = (inputmethodmanager) getapplicationcontext (). GetSystemservice (Context.input_method_service);
Im.hidesoftinputfromwindow (Comment_content.getwindowtoken (), 0);
Break
Case R.id.comment_send:sendcomment ();
Break
Default:break; }/** * Send comment */public void sendcomment () {if (Comment_content.gettext (). toString (). Equals ("")) {Toast.maket Ext (Getapplicationcontext (), "Comment cannot be empty!")
", Toast.length_short). Show ();
}else{//Generate comment data Comment Comment = new Comment ();
Comment.setname ("reviewer" + (data.size () +1) + ":");
Comment.setcontent (Comment_content.gettext (). toString ());
Adaptercomment.addcomment (comment);
Send out, empty the input box Comment_content.settext (""); Toast.maketext (Getapplicationcontext (), "Comment succeeded!"
", Toast.length_short). Show ();
}
}
}
Attention:
Because the Android phone type is miscellaneous, so some mobile phones will appear in the bottom of the input box and input method overlap, the following figure, the red circle of this part is not:
When this problem occurs, you can add a property to the corresponding activity in the Manifest.xml file
Android:windowsoftinputmode= "Statehidden|adjustresize"
In this way, the input method can be automatically adjusted to show the part of the red circle, the bottom of the input box and input method will not overlap.
4, the final effect chart is as follows
Hide the interface of an input box
Display the interface of the input box
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.