博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Button简单实例1
阅读量:7158 次
发布时间:2019-06-29

本文共 2485 字,大约阅读时间需要 8 分钟。

1.XML按钮定义

 显示:

2.后台代码创建控件并注册事件

import android.annotation.SuppressLint;import android.app.Activity;import android.app.ActionBar.LayoutParams;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.LinearLayout;/** * 简单动态产生按钮,并绑定事件 *  * @author zhongyang * */public class Button_Two extends Activity {    private LinearLayout parentLayout = null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // 指定布局面板        createParentLayout();        // 创建按钮        CreateButton();    }    // 产生布局面板    @SuppressLint("NewApi")    private void createParentLayout() {        parentLayout = new LinearLayout(this);        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,                LayoutParams.MATCH_PARENT);        parentLayout.setLayoutParams(layoutParams);        setContentView(parentLayout);    }    // 添加按钮并注册事件    @SuppressLint("NewApi")    private void CreateButton() {        Button btnOne = new Button(this);        btnOne.setText("显示其他Activity");        btnOne.setOnClickListener(new btnOneListener());        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,                LayoutParams.WRAP_CONTENT);        btnOne.setLayoutParams(layoutParams);        parentLayout.addView(btnOne);    }    // 按钮点击事件方法    class btnOneListener implements View.OnClickListener {        public void onClick(View v) {            Button thisBtn = (Button) v;            thisBtn.setWidth(100);            thisBtn.setText("按钮点击事件触发了");        }    }}

显示:

3.XML创建控件 注册click事件

import android.app.Activity;import android.content.res.ColorStateList;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;/** * 访问布局视图中的按钮并注册事件 * @author zhongyang * */public class ButtonThree extends Activity {    private TextView txtOne = null;    private Button btnOne = null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // 设置布局视图,初始化控件        setContentView(R.layout.button_three);        txtOne = (TextView) this.findViewById(R.id.txtOne);        btnOne = (Button) this.findViewById(R.id.btnOne);    }    // 按钮点击事件    public void btnOneClick(View view) {        Button btn = (Button) view;        btn.setTextSize(25);        // 修改TextView的值        txtOne.setText("按钮One触发点击事件");    }}

显示:

 

转载地址:http://qrhgl.baihongyu.com/

你可能感兴趣的文章
Linux常用命令——find
查看>>
数据中台专栏(三):数据质量分析及提升
查看>>
iOS多点触控与手势识别
查看>>
Sql server--索引
查看>>
UML建模工具
查看>>
视频合成软件哪个好,怎么把多个视频快速合并成一个视频
查看>>
在Linux系统中创建SSH服务器别名
查看>>
【JMS 4】spring 整合activemq
查看>>
PDF文档页码怎么设置
查看>>
java单例模式
查看>>
多线程基础 (八)NSOperation相关
查看>>
【已解决】PHP项目需求:在现有网站中每个页面增加一个get参数
查看>>
Linux下安装oracle10g全解
查看>>
软件分层思想
查看>>
JAVA测试实际代码多少行,注释多少行,空格多少行?
查看>>
css与css3对比
查看>>
day7-select Port multiplexing, multiple IO
查看>>
查看哪些用户登录了ftp服务器
查看>>
学习Nagios(1)监控门禁
查看>>
30个优秀的大自然风格网页设计作品欣赏
查看>>