GridLayout中动态加载的Button按钮如何触发

来源:互联网 发布:青少年行知实践园在哪 编辑:程序博客网 时间:2024/05/20 14:22

GridLayout是4.0新增的布局,我试了下,做了个计算器,不过写完之后觉得怎么按钮不可以触发,弄了会儿搞出来了,直接上代码:


布局activity_main.xml


<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="4"
    android:rowCount="6"
    tools:context=".MainActivity" >


    <TextView
        android:id="@+id/txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:layout_gravity="right"
        android:layout_marginLeft="4px"
        android:layout_marginRight="4px"
        android:background="#eee"
        android:padding="5px"
        android:text="0"
        android:textColor="#000"
        android:textSize="50sp" />


    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:text="清除" />


</GridLayout>


java代码:

package com.issay.gridlayouttest;


import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity
{


    GridLayout gridLayout;


    Button btn;


    TextView txt;


    // 定义16个按钮的文本
    String[] chars = new String[] {


    "7", "8", "9", "÷", "4", "5", "6", "×", "1", "2", "3", "-", ".", "0", "=",
            "+"


    };


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        gridLayout = (GridLayout) findViewById(R.id.root);


        btn = (Button) findViewById(R.id.btn);


        txt = (TextView) findViewById(R.id.txt);


        btn.setOnClickListener(new OnClickListener()
        {


            @Override
            public void onClick(View v)
            {


                txt.setText("0");


            }
        });


        for (int i = 0; i < chars.length; i++)
        {


            Button bn = new Button(this);


            bn.setText(chars[i]);


            bn.setId(i);


            bn.setTextSize(40);


            GridLayout.Spec rowSpec = GridLayout.spec(i / 4 + 2);


            GridLayout.Spec columnSpec = GridLayout.spec(i % 4);


            GridLayout.LayoutParams params = new GridLayout.LayoutParams(
                    rowSpec, columnSpec);


            params.setGravity(Gravity.FILL);


            gridLayout.addView(bn, params);
            if (bn.getId() == i)
            {


                bn.setOnClickListener(new OnClickListener()
                {


                    @Override
                    public void onClick(View v)
                    {


                        Toast.makeText(MainActivity.this, "按下" + v.getId(),
                                30000).show();


                    }
                });


            }


        }


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


}