常用功能

先声明创建一个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"><Buttonandroid:id="@+id/btn_textview"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="TextView"/><Buttonandroid:id="@+id/btn_button"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Button"/></LinearLayout>

MainActivity.java

package com.example.helloword;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;public class MainActivity extends AppCompatActivity {//声明空间private Button mBtnTextView;private Button mBtnButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mBtnTextView = findViewById(R.id.btn_textview);//找到空间mBtnTextView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {//跳转到TextView演示界面Intent intent = new Intent(MainActivity.this,TextViewActivity.class);startActivity(intent);}});mBtnButton = findViewById(R.id.btn_button);mBtnButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {//跳转到Button演示界面Intent intent = new Intent(MainActivity.this,ButtonActivity.class);startActivity(intent);}});}
}

再新建演示Button的Activity

activity_button.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:padding="20dp"><Buttonandroid:id="@+id/btn_1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="按钮1"android:textSize="20sp"android:textColor="#0066FF"android:background="#CCFFFF"/><Buttonandroid:id="@+id/btn_2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="按钮2"android:layout_below="@id/btn_1"android:layout_marginTop="20dp"android:textSize="20sp"android:textColor="#0066FF"android:background="@drawable/bg_btn2"/>
<!--    功能:圆角-->
<!--    1.按下图1,2步骤新建xml-->
<!--    2.在bg_btn2中完成设置-->
<!--    3.android:background导入--><Buttonandroid:id="@+id/btn_3"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="按钮3"android:textSize="20sp"android:textColor="#0066FF"android:background="@drawable/bg_btn3"android:layout_below="@id/btn_2"android:layout_marginTop="20dp"/>
<!--    功能:描边+圆角--><Buttonandroid:id="@+id/btn_4"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="按钮4"android:textSize="20sp"android:textColor="#0066FF"android:background="@drawable/bg_btn4"android:layout_below="@id/btn_3"android:layout_marginTop="20dp"android:onClick="showToast"/>
<!--    功能一:按压效果-->
<!--    1.按下图1,3的步骤新建xml-->
<!--    2.在bg_btn4中完成设置-->
<!--    功能二:点击事件方法1-->
<!--    1.写android:onClick="showToast"-->
<!--    2.在ButtonActivity.java中写这个方法--><Buttonandroid:id="@+id/btn_5"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="按钮5"android:textSize="20sp"android:textColor="#0066FF"android:background="#CCFFFF"android:layout_below="@id/btn_4"android:layout_marginTop="20dp"/>
<!--    功能:点击事件方法2-->
<!--    直接在在ButtonActivity.java中对btn_5进行操作--></RelativeLayout>

ButtonActivity.java

package com.example.helloword;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;public class ButtonActivity extends AppCompatActivity {private Button mBtn5;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_button);mBtn5 = (Button)findViewById(R.id.btn_5);mBtn5.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Toast.makeText(ButtonActivity.this,"按钮5被点击了",Toast.LENGTH_SHORT).show();}});}//    写showToast这个方法public void showToast(View view){Toast.makeText(this,"按钮4被点击了",Toast.LENGTH_SHORT).show();}
}

bg_btn2.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solidandroid:color="#FF99"/>
<!--    填充颜色--><cornersandroid:radius="15dp"/>
<!--    四个角设为圆角--></shape>

bg_btn3.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><strokeandroid:width="2dp"android:color="#FF0000"/>
<!--    描边--><cornersandroid:radius="15dp"/>
<!--    四个角设为圆角--></shape>

bg_btn4.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true">
<!--        按压效果--><shape><solid android:color="#FFFFFF"/><corners android:radius="15dp"/></shape></item><item android:state_pressed="false"><!--        没有按压的时候--><shape><solid android:color="#CC7A00"/><corners android:radius="15dp"/></shape></item></selector>


图1

图2

图3

最终效果:

Button练习 Android Emulator - Nexus

Android之Button练习相关推荐

  1. android 按钮放中间,Android实现button居中的方法

    本文实例讲述了Android实现button居中的方法.分享给大家供大家参考.具体如下: 通过在main.xml 或者其他xml 布局文件中布局Button的时候,选择Android:gravity= ...

  2. 自定义背景android,Android自定义Button并设置不同背景图片的方法

    本文实例讲述了Android自定义Button并设置不同背景图片的方法.分享给大家供大家参考,具体如下: 1.自定义MyButton类 public class MyButton extends Bu ...

  3. android onclick方法吗,Android中button的onClick事件几种方法

    Android中button的onClick事件几种方法 发布时间:2020-09-03 02:36:31 来源:脚本之家 阅读:100 作者:水桶前辈 Android中button的onClick事 ...

  4. Android Studio Button背景颜色无法修改

    关于Android Studio Button背景无法修改,一直呈现亮紫色 作为安卓初学者,发现Button的背景颜色无法修改,也没法链接到drawable的样式xml文件,前前后后折腾了好久,查阅了 ...

  5. android 自定义控件 焦点,Android 自定义Button按钮显示样式(正常、按下、获取焦点)...

    现在的用户对APP的外观看得很重要,如果APP内所有元件都用Android默认样式写,估计下面评论里就有一堆在骂UI丑的.今天学习自定义Button按钮样式.Button样式修改的是Button的背景 ...

  6. java.lang.ClassCastException:android.widget.Button cannot be cast to android.widget.ImageView

    今天遇到一个错误也不知道怎么回事,上网搜了一下: 出现的问题是:java.lang.ClassCastException:android.widget.Button cannot be cast to ...

  7. Android中Button组件的使用

    前言 安卓系统中,Button是程序和用户进行交互的一个重要控件,今天我们就来简单的对Button进行学习,其中Button组件是文本按钮(继承自TextView),而ImageButton是图像按钮 ...

  8. Android之Button样式

    一. 简单实例 src package cn.android.lyj;import android.app.Activity; import android.app.Dialog; import an ...

  9. Android UI Button 和GridView 的设计--优化(2)

    Android 按钮的UI设计,ListView 以及GridView的UI设计 一.按钮的状态 我们一般搞UI设计,按钮通常有三个状态:normal(正常状态);focus(焦点状态),presse ...

最新文章

  1. 如何遍历字符串中的单词?
  2. mysql could not create connection_mysql8.0 Could not create connection to database server.解决办法...
  3. 成功计算出文本类单词的概率
  4. 2012年生活方向盘
  5. Redis的两种持久化方式
  6. 1005 Spell It Right (20 分)——13行代码Ac
  7. 关于XtraGrid的CustomUnboundColumnData事件的触发条件
  8. 在线登记系统代码 php_PHP框架实现WebSocket在线聊天通讯系统
  9. 微课|玩转Python轻松过二级(1.6节):导入和使用标准库、扩展库对象
  10. python立方尾不变代码_对于这个蓝桥杯立方尾不变题我用java程序做的,正确结果应该是36,为什么我这样写结果就是12,如...
  11. WPF Rendering 2(硬件加速、软件加速)
  12. 【渝粤教育】电大中专消费者心理学基础作业 题库
  13. visio2010下载地址中文版本32位中文版本64位和激活密钥方法分享哦
  14. ubuntu查看显卡驱动以及其他驱动
  15. 最新wxid转扫一扫添加好友
  16. linux下载ccle数据,对CCLE数据库可以做的分析--转载
  17. 网桥(生成树网桥和源路由网桥)
  18. 安卓开发无线连接设备进行调试(adb)
  19. python使用IE浏览器
  20. listview 的首行固定内容标题且加粗显示(类似于表格的首行)的实现方法

热门文章

  1. Codeforces Round #236 (Div. 2) C. Searching for Graph(水构造)
  2. sqlite遇到database is locked问题的完美解决
  3. 使用CDN加速后网站不能使用HttpWebRequest提交数据
  4. python3librequest_python3.x學習之urilib.request簡單學習
  5. 查看某个进程的线程在干什么_有了多线程,为什么还要有协程?
  6. SqlServer两表之间:根据一个表的字段更新另一个表的字段
  7. Vim-latex 插件 的安装
  8. python3 + selenium 之窗口切换
  9. Tyvj P1015 公路骑 (DP)
  10. hdu 1027 输出第m个全排列(next_permutation)