AndroidStudio学习记录(3):操纵按钮控件Botton、ImageBotton

按钮控件是平时看到的,常用Botton和ImageButton控件,一般操纵 按钮来实现相应的命令,比如在手机上的查找 登录 注册,以及点击命令等等。

ImaBotton与Button的区别在于它没有文本,只有图片,需要制定图片路径

在activity_main.xml文件中,它们是这样使用的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn01"
android:onClick="btn02Click"
android:text="按钮1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn02"
android:text="按钮2"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="100dp"
android:id="@+id/imgBtn"
android:src="@mipmap/ic_launcher_round"
android:contentDescription="TODO" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"/>
</LinearLayout>

接下来是Java后台代码:

package com.aaa.my2;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;


import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.google.android.material.bottomnavigation.BottomNavigationItemView;

public class MainActivity extends AppCompatActivity {
    //定义变量;ALT+Enter;导入类
    Button btn01,btn02;
    ImageButton imgBtn;
    TextView tv_msg;


    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//------------------------------------------------------------------------------
        //获取变量给当前对象
        btn01=(Button) findViewById(R.id.btn01);
        btn02=(Button) findViewById(R.id.btn02);
        imgBtn=(ImageButton) findViewById(R.id.imgBtn);
        tv_msg=(TextView) findViewById(R.id.tv_msg);//

        //事件监听器
        btn01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                tv_msg.setText("这是图片按钮的事件");
            }

        });
        //定义一个方法;
    }
    //自行定义一个方法,这个放在OnCreate外,单独的方法,当单击产生响应
    public void btn02Click(View v) {
        tv_msg.setText(("这个是按钮2自定义的方法"));
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值