Android uses GridLayout grid layouts to make simple calculators app_android

Source: Internet
Author: User
Tags clear screen gettext numeric value stub first row

About GridLayout

Before the android4.0 version, if you want to achieve the grid layout, consider using the most common linearlayout layouts, but there are several problems with this arrangement:

1, can not be at the same time in the direction of the x,y axis control alignment.
2, when the multi-layer layout nesting will have performance problems.
3, not stable support for a number of tools to support the free editing of the layout.

Second, consider using table layout tabellayout, which arranges the contained elements in rows and columns, one TableRow object per action, or one view object, and in TableRow you can continue to add other controls. Each additional child control becomes a column. However, using this layout may cause problems where the control cannot be occupied by multiple rows or columns, and the rendering speed is not well guaranteed.

The above problems are solved by the GridLayout layout of the android4.0 above version. The GridLayout layout uses dashed lines to divide the layout into rows, columns, and cells, and also supports a control that has a staggered arrangement on rows and columns. and GridLayout use is actually similar to LinearLayout API, but is to modify the relevant tags, so for developers, mastering the GridLayout is very easy thing. GridLayout's layout strategy is simply divided into the following three sections:
First it is the same as the LinearLayout layout, which is divided into horizontal and vertical ways, the default is horizontal layout, a control from left to right next to a control, but by specifying android:columncount the number of columns to set the properties, the control will be automatically wrapped line. On the other hand, for child controls in the GridLayout layout, the display is set by default as Wrap_content, which only needs to be explicitly declared in the GridLayout layout.

Second, to specify that a control is displayed in a fixed row or column, you can simply set the Android:layout_row and Android:layout_column properties of the child control, but note that: android:layout_row= "0" Indicates that starting with the first row, android:layout_column= "0" represents the beginning of the first column, similar to the assignment of one-dimensional arrays in the programming language.

Finally, if you need to set a control to span more than one row or more columns, simply set the Android:layout_rowspan or Layout_columnspan property of the child control to a numeric value, and then set its Layout_gravity property to fill. The previous setting indicates the number of rows or columns that the control spans, and the latter setting indicates that the control fills the entire row or column that spans.

Calculator instance

Through the Android4.0 grid layout GridLayout to achieve a simple Calculator interface layout source is as follows (Welcome to guide the criticism):

Package com.android.xiong.gridlayoutTest; 
Import Java.math.BigDecimal; 
 
Import Java.util.regex.Pattern; 
 
Import com.android.xiong.gridlayouttest.r.id; 
Import Android.os.Bundle; 
Import android.app.Activity; 
Import Android.view.Menu; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
Import Android.widget.Button; 
Import Android.widget.EditText; 
Import Android.widget.GridLayout; 
 
Import Android.widget.TextView; 
 
  public class Mainactivity extends activity {private edittext print;  private static string fistnumber = "0"; The value that is entered first is private static string secondnumber = "0";/the second entered value private static 
 
  String num = "0";//display result private static int FLG = 0;//results Add once public Counts take = null; 
 
  Private int[] Btidtake = {r.id.txtdivide, r.id.txtx, R.id.txtmin, r.id.txttakesum}; 
 
  Private button[] Buttontake = new Button[btidtake.length]; Private int[] Btidnum = {r.id.txt0, r.id.txt1, R.id.txt2, R.id.txt3, R.ID.TXT4, R.ID.TXT5, R.id.txt6, R.id.txt7, R.id.txt8, R.id.txt9, R.ID.TXTSPL}; 
 
  Private button[] buttons = new Button[btidnum.length]; 
  Private int[] Btcl = {r.id.chars, R.id.charx, R.ID.TXTB, R.ID.TXTV}; 
  Private button[] Btcls = new Button[btcl.length]; 
 
  Private GridLayout gly; 
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
    Setcontentview (R.layout.activity_main); 
    Gly= (GridLayout) Findviewbyid (r.id.gly); 
    Print = (edittext) Findviewbyid (r.id.print); 
    Print.settext ("0"); 
    Print.setenabled (FALSE); 
    GetNumber get = new GetNumber (); 
      for (int i = 0; i < btidnum.length i++) {buttons[i] = (Button) Findviewbyid (btidnum[i)); 
    Buttons[i].setonclicklistener (get); 
    } Compute cm = new Compute (); 
      for (int i = 0; i < btidtake.length i++) {Buttontake[i] = (Button) Findviewbyid (btidtake[i)); 
    Buttontake[i].setonclicklistener (CM); } ButtOn eq = (Button) Findviewbyid (R.ID.TXTEQ);  Eq.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {if (FLG = 0) 
          {secondnumber = Print.gettext (). toString (); 
          if (take = = Counts.divide && secondnumber.equals ("0")) {Print.settext ("0 cannot be dividend"); else {num = take. 
            Values (Fistnumber, Secondnumber); 
            Fistnumber = num; 
            Secondnumber = "0"; 
            Print.settext (num); 
            FLG = 1; 
          Gly.setbackgroundresource (R.DRAWABLE.JZ); 
    } 
        } 
      } 
    }); 
    Button Cleargo = (button) Findviewbyid (R.id.cleargo); Cleargo.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO 
        auto-generated method Stub if (Num.length () > 1) {num = num.substring (0, Num.length ()-1); 
        else {num = "0"; 
        }Print.settext (num); 
    } 
    }); 
    Button clear = (Button) Findviewbyid (r.id.clear); Clear.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO 
        auto-generated method Stub num = "0"; 
        Fistnumber = Secondnumber = num; 
        Print.settext (num); 
      FLG = 0; 
    } 
    }); 
      for (int i = 0; i < btcl.length i++) {btcls[i] = (Button) Findviewbyid (btcl[i)); 
    Btcls[i].setonclicklistener (New Ontake ());  }//To EditText assign class GetNumber implements Onclicklistener {@Override public void OnClick (View 
      V) {//TODO auto-generated Method Stub if (FLG = = 1) num = "0"; 
        if (Num.equals ("0")) {Print.settext (""); num = V.getid () = = R.ID.TXTSPL? 
      "0": ""; 
      } String txt = ((Button) v). GetText (). toString (); Boolean s = pattern.matches ("-*" (\\d+).? 
      (\\d) * ", num + txt); num = s? (num + txt): num; 
      Gly.setbackgroundresource (r.drawable.js); 
    Print.settext (num);  
 
      The class Compute implements Onclicklistener {@Override public void OnClick (View arg0) { 
      Fistnumber = Print.gettext (). toString ();  
        TODO auto-generated Method Stub switch (Arg0.getid ()) {case r.id.txttakesum:take = Counts.add; 
      Break 
        Case r.id.txtmin:take = Counts.minus; 
      Break 
        Case r.id.txtx:take = counts.multiply; 
      Break 
        Case r.id.txtdivide:take = counts.divide; 
      Break 
      num = "0"; 
      FLG = 0; 
    Gly.setbackgroundresource (r.drawable.js);  Class Ontake implements Onclicklistener {@Override public void OnClick (View v) {//TODO 
        auto-generated method Stub switch (V.getid ()) {Case r.id.chars:num = "-" + num; 
      Break 
 Case R.ID.CHARX:       if (!num.equals ("0")) num = bigdecimal.valueof (1). Divide (new BigDecimal (num), 12,bigdecimal.round_up). Stript     
        Railingzeros (). toString (); 
      Break Case r.id.txtb:num = new BigDecimal (num). Divide (Bigdecimal.valueof (), 12,bigdecimal.round_up). Striptrailingzer 
        Os (). toString (); 
      Break 
        Case R.id.txtv:double NUMSS = math.sqrt (new BigDecimal (num). Doublevalue ()); int stratindex=numss.tostring (). Contains (".")? 
        Numss.tostring (). IndexOf ("."): 0; 
      num = numss.tostring (). Length () >13?numss.tostring (). substring (0, 12+stratindex): Numss.tostring (); 
      } print.settext (num); 
      flg=0; 
 
    num = "0"; }} @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; this adds items to th 
    E Action Bar if it is present. 
    Getmenuinflater (). Inflate (R.menu.main, menu); 
  return true; 
 } 
 
}
Package com.android.xiong.gridlayoutTest; 
 
Import Java.math.BigDecimal; 
 
public enum Counts { 
  ADD, minus, MULTIPLY, DIVIDE, MARK, ROOT; 
  public string Values (string num1, String num2) { 
    BigDecimal number1 = new BigDecimal (NUM1); 
    BigDecimal number2 = new BigDecimal (num2); 
    BigDecimal number = bigdecimal.valueof (0); 
    Switch (this) {case 
    ADD: Number 
      = Number1.add (number2); 
      break; 
    Case minus: Number 
      = Number1.subtract (number2); 
      break; 
    Case MULTIPLY: Number 
      = Number1.multiply (number2); 
      break; 
    Case DIVIDE: Number 
      = Number1.divide (number2,20,bigdecimal.round_up); 
      break; 
    Return Number.striptrailingzeros (). toString (); 
 
 


<gridlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Wrap_content "Android:layou" t_margintop= "40px" android:columncount= "5" android:rowcount= "6" tools:context= ". Mainactivity "> <edittext android:id=" @+id/print "android:layout_width=" Match_parent "Android:la" yout_height= "Wrap_content" android:layout_columnspan= "5" android:layout_marginleft= "2px" Android:layout_mar ginright= "2px" android:layout_row= "0" android:background= "#eee"/> <button android:id= "@+id/clea 
    rGO "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:layout_column=" "0" android:layout_row= "1" android:text= "--"/> <button android:id= "@+id/clear" Android:layout_wi Dth= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_Column= "3" android:layout_columnspan= "2" android:layout_row= "1" android:layout_gravity= "Fill_horizontal" android:text= "Clear Screen"/> <button android:id= "@+id/chars" android:layout_width= "Wrap_content" Andr 
 
  oid:layout_height= "Wrap_content" android:layout_column= "1" android:layout_row= "1" android:text= "-/+"/> <button android:id= "@+id/charx" android:layout_width= wrap_content "android:layout_height=" Wrap_con Tent "android:layout_column=" 2 "android:layout_row=" 1 "android:text=" 1/x "/>" <button Androi D:id= "@+id/txt7" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_co lumn= "0" android:layout_row= "2" android:text= "7"/> <button android:id= "@+id/txt8" Android: Layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_column= "1" android:layout_r ow= "2" Android:text= "8"/> <button android:id= "@+id/txt9" android:layout_width= "Wrap_content" android:layout_height 
    = "Wrap_content" android:layout_column= "2" android:layout_row= "2" android:text= "9"/> <button Android:id= "@+id/txtdivide" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Andr 
    Oid:layout_column= "3" android:layout_row= "2" android:text= "a"/> <button android:id= "@+id/txtb" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_column= "4" and roid:layout_row= "2" android:text= "%"/> <button android:id= "@+id/txt4" android:layout_width= "Wra" P_content "android:layout_height=" wrap_content "android:layout_column=" 0 "android:layout_row=" 3 "Andro id:text= "4"/> <button android:id= "@+id/txt5" android:layout_width= "Wrap_content" android:layout 
_height= "Wrap_content"    android:layout_column= "1" android:layout_row= "3" android:text= "5"/> <button android:id= "@+i 
    D/txt6 "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:layout_column=" "2" android:layout_row= "3" android:text= "6"/> <button android:id= "@+id/txtx" Android:layout_wi 
    Dth= "Wrap_content" android:layout_height= "Wrap_content" android:layout_column= "3" android:layout_row= "3" android:text= "X"/> <button android:id= "@+id/txtv" android:layout_width= "Wrap_content" Androi 
 
  d:layout_height= "Wrap_content" android:layout_column= "4" android:layout_row= "3" android:text= "√"/>  
    <button android:id= "@+id/txt1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:layout_column= "0" android:layout_row= "4" android:text= "1"/> <button android:id= "@ +id/txt2 "Android:lAyout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_column= "1" Android:layout_ro 
    w= "4" android:text= "2"/> <button android:id= "@+id/txt3" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_column= "2" android:layout_row= "4" android:text= "3"/& 
 
  Gt <button android:id= "@+id/txtmin" android:layout_width= wrap_content "android:layout_height=" wrap_content "Android:layout_column=" 3 "android:layout_row=" 4 "android:text="-"/> <button android:id=" @+id/txteq "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:layout_column=" "4" android:layout_gravity= "Fill_vertical" android:layout_row= "4" android:layout_rowspan= "2" android:t ext= "="/> <button android:id= "@+id/txt0" android:layout_width= "Wrap_content" Android:layout_hei Ght= "Wrap_conteNT "android:layout_column=" 0 "android:layout_columnspan=" 2 "android:layout_gravity=" Fill_horizontal "a" ndroid:layout_row= "5" android:text= "0"/> <button android:id= "@+id/txtspl" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_column= "2" android:layout_row= "5" a ndroid:text= "."/> <button android:id= "@+id/txttakesum" android:layout_width= "Wrap_content" Andr 
 
oid:layout_height= "Wrap_content" android:layout_column= "3" android:layout_row= "5" android:text= "+"/>  </GridLayout>


Related Article

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.