Android开发学习【Button控件】

Button

按钮控件Button由TextView派生而来,它们之间的区别有:
Button拥有默认的按钮背景,而TextView默认无背景;
Button的内部文本默认居中对齐,而TextView的内部文本默认靠左对齐;
Button会默认将英文字母转为大写,而TextView保持原始的英文大小写;

与TextView相比,Button增加了两个新属性

textAllCaps属性,它指定了是否将英文字母转为大写,为true是表示自动转为大写,为false表示不做大写转换。
在这里插入图片描述

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下面的英文默认大写"
        android:textColor="@color/black"
        android:textSize="17sp"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World"
        android:textColor="@color/black"
        android:textAllCaps="false" 
        android:textSize="17sp"/>

onClick属性,它用来接管用户的点击动作,指定了点击按钮时要触发哪个方法;

书写一个点击显示当前时间的按钮

在这里插入图片描述

获取当前时间的java类

package com.example.study01.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
    public static String getNowTime(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        return simpleDateFormat.format(new Date());
    }
}

xml主界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ButtonStyleActivity"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击下方按钮获取当前时间"
        android:textColor="@color/black"
        android:textSize="17sp"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World"
        android:textColor="@color/black"
        android:textAllCaps="false"
        android:textSize="17sp"
        android:onClick="doClick"/>
</LinearLayout>

MainActivity类

package com.example.study01;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.study01.util.DateUtil;

public class ButtonStyleActivity extends AppCompatActivity {

    private TextView tv_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_style);
        tv_id = findViewById(R.id.tv_id);
    }

    public void doClick(View view) {
        String desc = String.format("%s 您点击了按钮: %s",DateUtil.getNowTime(),((Button)view).getText());
        tv_id.setText(desc);
    }
}

点击事件和长按事件

监听器,意思是专门监听控件的动作行为。只有控件发生了指定的动作,监听器才会触发开关去执行对应的代码逻辑。
按钮控件有两种常用的监听器:
点击监听器,通过setOnClickListener方法设置。按钮被按住少于500毫秒时,会触发点击事件。
长按监听器,通过setOnLongClickListener方法设置。按钮被按住超过500毫秒时,会触发长按事件。

使用setOnClickListener方法设置监听器获取当前时间

在这里插入图片描述

package com.example.study01;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.study01.util.DateUtil;

public class ButtonClickActivity extends AppCompatActivity implements View.OnClickListener{

    private static TextView tv_btn_time1;
    private TextView tv_btn_time2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_click);
        Button btn_click1 = findViewById(R.id.btn_click1);
        tv_btn_time1 = findViewById(R.id.tv_btn_time1);
        btn_click1.setOnClickListener(new MyOnClickListener());
        Button btn_click2 = findViewById(R.id.btn_click2);
        btn_click2.setOnClickListener(this);
        tv_btn_time2 = findViewById(R.id.tv_btn_time2);
    }

    @Override
    public void onClick(View v) {
        String desc = String.format("%s 您点击了按钮: %s", DateUtil.getNowTime(),((Button)v).getText());
        tv_btn_time2.setText(desc);
    }
	/*这里使用静态类防止内存泄漏*/
    static class MyOnClickListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            String desc = String.format("%s 您点击了按钮: %s", DateUtil.getNowTime(),((Button)v).getText());
            tv_btn_time1.setText(desc);
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ButtonClickActivity"
    android:orientation="vertical">
    <Button
        android:id="@+id/btn_click1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="获取当前时间1"/>
    <TextView
        android:id="@+id/tv_btn_time1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""/>

    <Button
        android:id="@+id/btn_click2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="获取当前时间2"/>
    <TextView
        android:id="@+id/tv_btn_time2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""/>
</LinearLayout>

使用setOnLongClickListener方法设置监听器获取当前时间

在这里插入图片描述

package com.example.study01;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.study01.util.DateUtil;

public class ButtonLongClickActivity extends AppCompatActivity {

    private TextView tv_long;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_long_click);
        tv_long = findViewById(R.id.tv_long);
        Button btn_long_click = findViewById(R.id.btn_long_click);
        btn_long_click.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                String desc = String.format("%s 您点击了按钮: %s", DateUtil.getNowTime(),((Button)v).getText());
                tv_long.setText(desc);
                return true;
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ButtonLongClickActivity"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_long_click"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="长按点击事件"/>
    <TextView
        android:id="@+id/tv_long"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="17sp"/>
</LinearLayout>

图像视图ImageView

图像视图展示的图片通常位于res/drawable***目录,设置图像视图的显示图片有两种方
式:
在XML文件中,通过属性android:src设置图片资源,属性值格式形如“@drawable/不含扩展名的图片名称”
在Java代码中,调用setImageResource方法设置图片资源,方法参数格式形如“R.drawable.不含扩展名的图片名称”
在这里插入图片描述

在这里插入图片描述

图像按钮ImageButton

ImageButton是显示图片的图像按钮,但它继承自ImageView,而非继承Button。
ImageButton和Button之间的区别有:
Button既可显示文本也可显示图片,ImageButton只能显示图片不能显示文本。
ImageButton上的图像可按比例缩放,而Button通过背景设置的图像会拉伸变形。
Button只能靠背景显示一张图片,而ImageButton可分别在前景和背景显示图片,从而实现两张图片叠加的效果。
在这里插入图片描述

同时展示文本与图像

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值