Button简单实例1

1.XML按钮定义

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:layout_marginRight="50dp"
    android:text="第一个自定义按钮"
    android:textColor="#ff0000" />

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="100dp"
    android:background="#0000ff"
    android:fontFamily="宋体"
    android:text="第二个中文按钮"
    android:textColor="#ffffff" />

</RelativeLayout>

 显示:

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事件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<TextView
    android:id="@+id/txtOne"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30px"
    android:background="#000000"
    android:paddingBottom="20px"
    android:paddingLeft="20px"
    android:paddingTop="20px"
    android:text="按钮点击之前"
    android:textColor="#ffffff" />

<Button
    android:id="@+id/btnOne"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:layout_marginTop="30px"
    android:onClick="btnOneClick"
    android:text="按钮One"
    android:textColor="#00ff00"
    android:textSize="20px" />

</LinearLayout>
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触发点击事件");
    }
}

显示:

 

一:使用匿名内部类的形式进行设置       1.首先需要得到id,即必须清楚button的id值。        2.使用setOnClickListener,如下图所示    二:在XML文件中定义OnClick属性,在java代码中对应方法。       在button下加上如下代码android:onClick=skip(skip即为方法对应的名字,然后在java代码中在定义具体的方法)                 实现的结果如下: --------------------- 作者:xd1501013 来源:优快云 原文:https://blog.youkuaiyun.com/xd15010130025/article/details/77604329 版权声明:本文为博主原创文章,转载请附上博文链接! Button按钮四种监听(二) 继续上次的,我们接下来要学习另外两种方式,三:Activity实现onClickListener接口;四:其他类实现onClickListener接口。    三:Activity实现onClickListener接口      1.直接在Activity上添加          public class MainActivity extends Activity implements OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn=(Button)findViewById(R.id.button1); btn.setOnClickListener(this); } 再创建方法      public void onClick(View v) { // TODO 自动生成的方法存根 Log.i("tag", "点击了button按钮"); } 实现效果    四:其他类实现onClickListener接口       1.创建一个其他类 class mylistener implements OnClickListener{ @Override public void onClick(View v) { // TODO 自动生成的方法存根 Log.i("tag", "点击了button按钮"); } }      二: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn=(Button)findViewById(R.id.button1); btn.setOnClickListener(new mylistener()); } 即可实现。 如果两个按钮怎么办????     用v.getId得到id值,然后用case来处理不同的情况.   --------------------- 作者:xd1501013 来源:优快云 原文:https://blog.youkuaiyun.com/xd15010130025/article/details/77803332 版权声明:本文为博主原创文章,转载请附上博文链接!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值