Android之activity中建立控制項

來源:互聯網
上載者:User

標籤:btn   入行   gre   res   view   odi   linear   ack   create   

瞭解了5大布局,我們會發現這些布局都是靜態,如何讓系統自動產生控制項呢?這就需要activity來幫忙了

  今天我們講的就是用activity建立布局

  用案例來說吧!

  實現一個輸入行和列自動產生表格並產生背景顏色

  效果

    

 

  代碼如下:

  

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical"          >        <EditText                        android:id="@+id/text_row"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_gravity="left|top"            android:ems="10"            android:hint="@string/text_row"            android:inputType="number">            <requestFocus />        </EditText>        <EditText            android:id="@+id/text_column"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_gravity="left|top"            android:ems="10"            android:hint="@string/text_column"            android:inputType="number"          />                <Button            android:id="@+id/btn_commit"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_gravity="center_vertical|center_horizontal"            android:text="@string/btn_commit"            />       </LinearLayout>     <TableLayout        android:layout_weight="1"        android:id="@+id/tablelayout"        android:layout_width="match_parent"        android:layout_height="wrap_content" >                  </TableLayout></LinearLayout>

 

 1 package com.example.linearlayout; 2  3 import android.app.Activity; 4 import android.graphics.Color; 5 import android.os.Bundle; 6 import android.view.Menu; 7 import android.view.MenuItem; 8 import android.view.View; 9 import android.view.View.OnClickListener;10 import android.widget.Button;11 import android.widget.LinearLayout;12 import android.widget.TableLayout;13 import android.widget.TableRow;14 import android.widget.TextView;15 import android.widget.Toast;16 17 18 public class MainActivity extends Activity {19     private TextView text_column;20     private TextView  text_row ;21     private TableLayout tabLay;22     private int length = 64;23     @Override24     protected void onCreate(Bundle savedInstanceState) {25         super.onCreate(savedInstanceState);26         setContentView(R.layout.layout2);27        //擷取控制項按鈕28        Button btn_commit = (Button) findViewById(R.id.btn_commit);29        
      //擷取控制項textview30 text_column = (TextView) findViewById(R.id.text_column);31 text_row= (TextView) findViewById(R.id.text_row);32 33 //給按鈕設定點擊事件 34 btn_commit.setOnClickListener(new OnClickListener(){35 36 @Override37 public void onClick(View v) {38   

              //擷取文本值39 String column = text_column.getText().toString().trim();40 String row = text_row.getText().toString().trim();41    //判斷是否為空白42 if(column.length() != 0 && row.length() != 0){43 //擷取控制項布局44 tabLay= (TableLayout) findViewById(R.id.tablelayout);45 46 //先清空47 tabLay.removeAllViewsInLayout();48 tabLay.setStretchAllColumns(true);49 //強轉整型50 int x =Integer.parseInt(text_column.getText().toString()); //??51 int y =Integer.parseInt(text_row.getText().toString()); 52 53 54   //for迴圈嵌套55 for(int m = 0 ; m < y; m++ ){56 57 TableRow tr = new TableRow(MainActivity.this);58 59 for(int n = 0 ; n < x ; n++ ){60 61 TextView tv = new TextView(MainActivity.this);62 63 //tv.setText("("+n+","+m+")");64 tv.setText(" ");65 tr.addView(tv);66   //擷取參數,設定背景顏色值67 int result = m*n +2*n-1;68 int red = result / 16/16 ;69 int green = ( result - red*16*16)/16;70 int blue = result - red*16*16 - green*16;71 72 tv.setBackgroundColor(Color.rgb(red*16, green*16, blue*16));73 74 75 }76 tabLay.addView(tr);77 78 }79 80 }else{81 Toast.makeText(MainActivity.this,"請輸入行和列",1).show();82 }83 } 84 });85 86 }87 }

 

    

Android之activity中建立控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.