android button 添加事件_Android——button(3)

一、简介

1.普通button

2.ImgeButton(带图片的button)

3.button(图片+文字)

4.ToggleButton (具有选择性的)

5.Switch

二、界面显示

038d001bf9b76bb58d427429c9182a65.png

三、布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="图标按钮:"/>
    <ImageButton
        android:id="@+id/button2"
        android:layout_marginLeft="50dp"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/ro2"/>
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮3"
        android:drawableLeft="@drawable/get"/>
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="这里是显示内容"/>
    <!--默认为未选中的状态。textOn,textOff,分别表示在选中和未选中状态所呈现的状态-->
    <ToggleButton
        android:layout_marginTop="20dp"
        android:id="@+id/togButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="已选中"
        android:textOff="未选中"/>
    <Switch
        android:id="@+id/btn_switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="关"
        android:textOn="开"/>
    <TextView
        android:layout_marginTop="20dp"
        android:id="@+id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text=""
        android:gravity="center"/>


</LinearLayout>

四、事件代码

package com.example.asus.button_two_test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {
    //声明变量
    private Button button1,button3;
    private ImageButton button2;
    private TextView text1,text2;
    private ToggleButton togButton;
    private Switch btn_switch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //当界面被创建时,就可以为变量赋值定义
        button1=(Button)findViewById(R.id.button1);
        button2=(ImageButton)findViewById(R.id.button2);
        button3=(Button)findViewById(R.id.button3);
        togButton=(ToggleButton)findViewById(R.id.togButton);
        btn_switch=(Switch)findViewById(R.id.btn_switch);
        //
        text1=(TextView)findViewById(R.id.text1);
        text2=(TextView)findViewById(R.id.text2);
        //设置响应事件
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text1.setText("按钮1被点击,一个简单的button");
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text1.setText("按钮2被点击,一个图片button");
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text1.setText("按钮3被点击,一个混合型button");
            }
        });
        togButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(((ToggleButton)view).isChecked()){
                    text2.setText("togButton被选中");
                }else{
                    text2.setText("togButton未被选中");
                }
            }
        });
        btn_switch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(((Switch)view).isChecked()){
                    text2.setText("switch被选中");
                }else{
                    text2.setText("switch未被选中");
                }
            }
        });


    }
}

有些时间段,熬过来就是大佬,熬不过来连菜鸟都不是

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值