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;
}
}
- GridLayout中动态加载的Button按钮如何触发
- 利用Java代码给gridLayout动态添加button按钮
- EasyUI动态加载button按钮
- 点击button按钮动态加载listView
- 如何在页面Page_Load时自动触发Button按钮的Command事件
- 动态按钮Button的实现
- 页面中有两个BUTTON,如何按回车触发TEXTBOX所属的BUTTON
- asp.net中TextBox里面实现回车触发按钮button的解决方案
- asp.net中如何回车触发指定按钮的事件
- 如何解决jsp中button按钮的超连接问题!
- VC下动态创建了BUTTON按钮后,如何设置按钮的Modal Frame风格???
- 触发Button按钮事件的三种方式
- 触发Button按钮事件的三种方式
- android 开发-捕捉按钮的触发(Button 动作)
- android 触发Button按钮事件的三种方式
- Android的Button按钮,ACTION_UP事件不触发解决方案
- Android的Button按钮,ACTION_UP事件不触发解决方案
- Android的Button按钮,ACTION_UP事件不触发解决方案
- 技术人员如何面试?
- hadoop远程客户端安装配置、多用户权限配置
- redis安装与使用
- HTTP协议 (七) Cookie
- 动态规划 之 序列专题
- GridLayout中动态加载的Button按钮如何触发
- Matlab floor函数与round的区别
- C语言mysql常用函数
- hdu1251统计难题(字典树)
- 如何获取网页源码
- php 中文亂碼問題解決
- KD树+BBF+KNN使用C#实现(1)
- 嵌入式学习步骤
- hdu4565之矩阵快速幂