Android implementation of the Nine Sudoku (the various equal space in the GridView) method _android

Source: Internet
Author: User

This article describes the Android implementation of the Nine Sudoku (the GridView in the Divided Space) method. Share to everyone for your reference. Specifically as follows:

The project needs to be done with a nine Sudoku (also not necessarily 9, 4 sudoku, 16 sudoku, 4x3 sudoku ...). ), sealing a Sudoku, which can be based on the space allocated to it from the movement of the regulation of the size of the palace.

Integration from the tablelayout, so if you use the package directly on the designer, you need to remove the TableRow that you want to add to it automatically.

The class name is Advancedgridview and the code is as follows:

Import Android.content.Context; 
Import Android.util.AttributeSet; 
Import Android.view.View; 
Import Android.widget.BaseAdapter; 
Import Android.widget.Button; 
Import Android.widget.TableLayout; 
Import Android.widget.TableRow; /** * Advancedgridview * @author Robintang * @time 2012-10-15/public class Advancedgridview extends 
  t {//private static final String tag = "Advancedgridview"; private int rownum = 0; Row number private int colnum = 0;  
  Col Number private Baseadapter adapter = null; 
  private context = NULL; 
    Public Advancedgridview {Super (context); 
  Initthis (context, NULL); 
    Public Advancedgridview (context, AttributeSet attrs) {Super (context, attrs); 
  Initthis (context, attrs); 
    private void Initthis (context context, AttributeSet attrs) {this.context = context; 
      if (This.gettag ()!= null) {string ATB = (String) this.gettag (); int IX = Atb.indeXOf (', '); 
        if (ix > 0) {rownum = Integer.parseint (atb.substring (0, ix)); 
      Colnum = Integer.parseint (atb.substring (ix+1, Atb.length ())); 
    } if (rownum <= 0) rownum = 3; 
    if (colnum <= 0) colnum = 3; 
      if (This.isineditmode ()) {this.removeallviews (); 
        for (int y=0; y<rownum; ++y) {TableRow row = new TableRow (context); 
        Row.setlayoutparams (New Layoutparams (Layoutparams.fill_parent, Layoutparams.fill_parent, 1.0f)); 
          for (int x=0; x<colnum; ++x) {View button = New button (context); 
        Row.addview (Button, new Tablerow.layoutparams (Layoutparams.match_parent, Layoutparams.match_parent, 1.0f)); 
      } this.addview (row); 
  }} public Baseadapter Getadapter () {return adapter; public void Setadapter (Baseadapter adapter) {if (adapter!= null) {if (Adapter.getcount () < This.rownum *this.colnum) {throw nEW IllegalArgumentException ("The View count of adapter is less than this GridView ' s items"); 
      } this.removeallviews (); 
        for (int y=0; y<rownum; ++y) {TableRow row = new TableRow (context); 
        Row.setlayoutparams (New Layoutparams (Layoutparams.fill_parent, Layoutparams.fill_parent, 1.0f)); 
          for (int x=0; x<colnum; ++x) {View view = Adapter.getview (Y*colnum+x, this, row); 
        Row.addview (view, New Tablerow.layoutparams (Layoutparams.match_parent, Layoutparams.match_parent, 1.0f)); 
      } this.addview (row); 
  } This.adapter = adapter; 
  public int Getrownum () {return rownum; 
  The public void setrownum (int rownum) {this.rownum = rownum; 
  public int Getcolnum () {return colnum; 
  The public void setcolnum (int colnum) {this.colnum = Colnum;

 } 
}

If you want to see the Sudoku effect at design time, you can set the number of columns on the Tag property of the space. For example, I would like to see the Sudoku of 3x3 to be set to "3,3", the following figure, of course, you can also use Setrownum () and Setcolnum () in the code to set, but call these two methods before you set up the adapter.

I hope this article will help you with your Android program.

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.