Package com.example.textviewfold; Import android.app.Activity; Import Android.os.Bundle; Import Android.view.View; Import Android.view.View.OnClickListener; Import Android.view.ViewTreeObserver; Import Android.view.ViewTreeObserver.OnPreDrawListener; Import Android.widget.Button; Import Android.widget.FrameLayout; Import Android.widget.ImageView; Import Android.widget.TextView; public class Mainactivity extends activity implements Onclicklistener { Private Button Bt_more; Private Framelayout Fl_desc; Private TextView Tv_desc_short; Private TextView Tv_desc_long; Private Boolean isinit = false; Private Boolean isshowshorttext = true; Private ImageView Iv_more_line; @Override protected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Findview (); Initview (); Setlistener (); } private void Setlistener () { Bt_more.setonclicklistener (this); } private void Initview () { String content = "Sina Science and technology news Beijing time July 25, in the morning of the new product launch, Google released the Android 4.3 version, code-named still" jelly beans (Jelly bean). Today's new generation of Nexus 7 will be equipped with the operating system, and Nexus Series devices can receive OTA push updates today. The Android 4.3 operating system adds a range of features. The first is the multiuser Settings feature, including the restricted file (Restricted profiles) feature for child protection. Users can limit the application content to prevent children from seeing inappropriate content when using the application, or to purchase advertisements in inappropriate applications. This feature is similar to the Microsoft Windows Phone's "Children's Park (Microsoft's Kid ' Corner)" feature. The second upgrade is the smart Bluetooth (Bluetooth Smart) feature, the Low-power bluetooth (Bluetooth low energy). "; Tv_desc_short.settext (content); Tv_desc_long.settext (content); Viewtreeobserver vto = Fl_desc.getviewtreeobserver (); Vto.addonpredrawlistener (New Onpredrawlistener () { @Override public Boolean Onpredraw () { if (isinit) return true; if (Mesuredescription (Tv_desc_short, Tv_desc_long)) { Iv_more_line.setvisibility (view.visible); Bt_more.setvisibility (view.visible); } Isinit = true; return true; } }); } /** * Calculate whether the descriptive information is too long */ Private Boolean mesuredescription (TextView Shortview, TextView longView) { Final int shortheight = Shortview.getheight (); Final int longheight = Longview.getheight (); if (Longheight > Shortheight) { Shortview.setvisibility (view.visible); Longview.setvisibility (View.gone); return true; } Shortview.setvisibility (View.gone); Longview.setvisibility (view.visible); return false; } private void Findview () { Fl_desc = (framelayout) Findviewbyid (R.ID.FL_DESC); Tv_desc_short = (TextView) Findviewbyid (R.id.tv_desc_short); Tv_desc_long = (TextView) Findviewbyid (R.id.tv_desc_long); Bt_more = (Button) Findviewbyid (R.id.bt_more); Iv_more_line = (ImageView) Findviewbyid (r.id.iv_more_line); } @Override public void OnClick (View v) { Switch (V.getid ()) { Case R.id.bt_more: if (Isshowshorttext) { Tv_desc_short.setvisibility (View.gone); Tv_desc_long.setvisibility (view.visible); } else { Tv_desc_short.setvisibility (view.visible); Tv_desc_long.setvisibility (View.gone); } Tooglemorebutton (Bt_more); Isshowshorttext =!isshowshorttext; Break Default Break } } /** * Change the text of the button "more" */ private void Tooglemorebutton (Button btn) { String text = (string) btn.gettext (); String MoreText = getString (R.string.label_more); String Lesstext = getString (r.string.label_less); if (moretext.equals (text)) { Btn.settext (Lesstext); } else { Btn.settext (MoreText); } } } |