android按钮

In this tutorial, we will look into Android Button. We will create android studio application and look into various things about button designing, styling, click events etc.

在本教程中,我们将研究Android Button。 我们将创建android studio应用程序,并研究与按钮设计,样式,点击事件等有关的各种事情。

Android按钮 (Android Button)

Android Buttons are GUI components that the users can click upon to either goto the next screen, confirm an option/trigger any event created by you, the Android Developer.
The android.widget.Button is a subclass of Android TextView.

Buttons in Android can be of the following forms:

Android Buttons是GUI组件,用户可以单击它们进入下一个屏幕,确认选项/触发由您(Android开发人员)创建的任何事件。
android.widget.Button是Android TextView的子类。

Android中的按钮可以采用以下形式:

  • RadioButton单选按钮
  • ToggleButton切换按钮
  • ImageButton图像按钮
  • FloatingActionButtonFloatingActionButton

In the following sections, we’ll be discussing the default button class, creating it programmatically, styling it.

在以下各节中,我们将讨论默认按钮类,以编程方式创建它,并设置其样式。

在布局中创建按钮 (Creating Button in Layout)

We can define the Button widget in our layout file in Android Studio in the following way.

我们可以通过以下方式在Android Studio中的布局文件中定义Button小部件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"xmlns:tools="https://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center"tools:context="com.journaldev.androidbutton.MainActivity"><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="DEFAULT BUTTON"android:onClick="clickMe"/></LinearLayout>

In the above code, we’ve wrapped our Button widget inside a LinearLayout
The id represents the unique identifier.
The onClick attribute requires the method name as the value. This method name should be defined in the corresponding activity of the layout.
It’ll get triggered when the button is clicked.
Setting android:background as a color would remove the selection animation from the button.
We can set an image inside the button alongise the text by using the attribute android:drawableLeft to set the image to the left of the text.
Our MainActivity.java class looks like this:

在上面的代码中,我们将Button小部件包装在LinearLayout中
id表示唯一标识符。
onClick属性需要将方法名称作为值。 该方法名称应在布局的相应活动中定义。
单击该按钮时将触发它。
android:background设置为颜色会从按钮中删除选择动画。
我们可以使用属性android:drawableLeft将图像设置在文本的左侧,从而在按钮内设置图像。
我们的MainActivity.java类如下所示:

package com.journaldev.androidbutton;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void clickMe(View view){Toast.makeText(getApplicationContext(),"Button is clicked",Toast.LENGTH_LONG).show();}
}

On clicking the button a Toast notification would be displayed onto the screen.

单击按钮后, Toast通知将显示在屏幕上。

以编程方式创建按钮 (Create Button Programmatically)

Let’s set the attributes on the button programmatically and create one button programmatically.

让我们以编程方式设置按钮的属性,并以编程方式创建一个按钮。

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button  = findViewById(R.id.button);button.setTextColor(Color.RED);button.setClickable(false);}

We hook the xml button id to the corresponding instance in the class using the findViewById method.
setTextColor() is used to set the text color,
setClickable accepts a boolean value. Setting it to false would make the button not clickable and the clickMe method won’t be triggered.

我们使用findViewById方法将xml按钮ID挂钩到类中的相应实例。
setTextColor()用于设置文本颜色,
setClickable接受布尔值。 将其设置为false会使按钮不可单击,并且不会触发clickMe方法。

以编程方式创建按钮 (Creating Button Programmatically)

The following code creates a button programmatically and adds it to the root hierarchy.

以下代码以编程方式创建一个按钮,并将其添加到根层次结构中。

Button programmaticButton = new Button(this);programmaticButton.setId(R.id.button2);programmaticButton.setText("CREATED PROGRAMMATICALLY");programmaticButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(getApplicationContext(),"Programmatic Button is clicked",Toast.LENGTH_LONG).show();}});LinearLayout linearLayout = findViewById(R.id.linearLayout);linearLayout.addView(programmaticButton);

We can set an ID programmatically using setId. We can create ids in the folder res | values | ids.xml as shown below:

我们可以使用setId以编程方式设置ID。 我们可以在文件夹res |中创建ID。 价值观| ids.xml如下所示:

setOnClickListener has an interface callback that listens for the click and triggers the onClick method whenever the button is clicked.

setOnClickListener具有接口回调,该回调侦听单击并在单击按钮时触发onClick方法。

Now let’s add another buttons in our layout programmatically in our MainActivity.java class.

现在,让我们在MainActivity.java类中以编程方式在布局中添加另一个按钮。

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button = findViewById(R.id.button);button.setTextColor(Color.RED);button.setClickable(false);//Creating a Button programmaticallyButton programmaticButton = new Button(this);programmaticButton.setId(R.id.button2);programmaticButton.setText("CREATED PROGRAMMATICALLY");programmaticButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(getApplicationContext(), "Programmatic Button is clicked", Toast.LENGTH_LONG).show();}});LinearLayout linearLayout = findViewById(R.id.linearLayout);linearLayout.addView(programmaticButton);programmaticButton = new Button(this);programmaticButton.setId(R.id.button3);programmaticButton.setText(getResources().getString(R.string.another_button));programmaticButton.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent));programmaticButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(getApplicationContext(), "Another Programmatic Button is clicked", Toast.LENGTH_LONG).show();}});}

setBackgroundColor() method is to set a color on the button programmatically.
Ideally, we should use string resources to set text on the button instead of hardcoding them in the java class itself.
In the above code, we’ve created two buttons and each of them has a separate interface callback for the listener. This means we’ll have a different object created in the memory for each of the buttons. This would add up in the heap memory and eventually the application would use more memory than required from the device. We can fix this performance issue by making the class implement the View.OnClickListener itself.

setBackgroundColor()方法用于以编程方式在按钮上设置颜色。
理想情况下,我们应该使用字符串资源在按钮上设置文本,而不是在Java类本身中对其进行硬编码。
在上面的代码中,我们创建了两个按钮,每个按钮都有一个用于侦听器的单独的接口回调。 这意味着我们将在内存中为每个按钮创建一个不同的对象。 这将增加堆内存,最终应用程序将使用比设备所需更多的内存。 我们可以通过使类实现View.OnClickListener本身来解决此性能问题。

Instead of using separate setOnClickListener callbacks for every Button, implement the interface on the class itself.
不必对每个Button使用单独的setOnClickListener回调,而应在类本身上实现接口。

So with the View.OnClickListener implemented in the class itself, our MainActivity.java class would look like this:

因此,通过在类本身中实现View.OnClickListener ,我们的MainActivity.java类将如下所示:

package com.journaldev.androidbutton;import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;public class MainActivity extends AppCompatActivity implements View.OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button = findViewById(R.id.button);button.setTextColor(Color.RED);button.setClickable(false);//Creating a Button programmaticallyButton programmaticButton = new Button(this);programmaticButton.setId(R.id.button2);programmaticButton.setText("CREATED PROGRAMMATICALLY");programmaticButton.setOnClickListener(this);LinearLayout linearLayout = findViewById(R.id.linearLayout);linearLayout.addView(programmaticButton);programmaticButton = new Button(this);programmaticButton.setId(R.id.button3);programmaticButton.setText(getResources().getString(R.string.another_button));programmaticButton.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent));programmaticButton.setOnClickListener(this);linearLayout.addView(programmaticButton);}public void clickMe(View view) {Toast.makeText(getApplicationContext(), "Button is clicked", Toast.LENGTH_LONG).show();}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.button2:Toast.makeText(getApplicationContext(), "Programmatic Button is clicked", Toast.LENGTH_LONG).show();break;case R.id.button3:Toast.makeText(getApplicationContext(), "Another Programmatic Button is clicked", Toast.LENGTH_LONG).show();break;}}
}

We’ve set each of the setOnClickListener to the common View.OnClickListener interface using this. In the switch block we detect the Button view that was clicked using their id.
This saves a lot of memory!

我们已使用this将每个setOnClickListener设置为公共View.OnClickListener接口。 在开关块中,我们检测到使用其ID单击的Button视图。
这样可以节省大量内存!

设定文字大小 (Setting Text Size)

Text size of a button is typically set in sp units.

按钮的文本大小通常以sp单位设置。

<Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="DEFAULT BUTTON"android:textSize="20sp"android:background="#147D03"android:onClick="clickMe"/>

OR do the following in your activity.

或者在您的活动中执行以下操作。

Button button = findViewById(R.id.button);
button.setTextColor(Color.RED);
button.setClickable(false);
button.setTextSize(22); //size in float

We’ve added a few more buttons to the layout and attached their listeners in the Activity.
Following is the output of the application in action.

我们在布局中添加了一些按钮,并在“活动”中附加了其侦听器。
以下是实际应用程序的输出。

This brings an end to this tutorial. You can find the source code for the Android Button Project below.
In the next tutorial, we’ll be discussing the Button states and selectors.

本教程到此结束。 您可以在下面找到Android Button项目的源代码。
在下一个教程中,我们将讨论Button状态和选择器。

Download Android Button Project下载Android Button项目

翻译自: https://www.journaldev.com/19838/android-button

android按钮

android按钮_Android按钮相关推荐

  1. android 置灰不可点击,Android Studio 运行按钮灰色的完美解决方法

    Android Studio 运行按钮灰色的完美解决方法 今天新建项目的时候突然发现编译后运行按钮为灰色. 解决方案:第一步:点击图中的Add Configuration,出来如下界面 第二步:点+号 ...

  2. android切换字体颜色,Android开发实现按钮点击切换背景并修改文字颜色的方法

    本文实例讲述了Android开发实现按钮点击切换背景并修改文字颜色的方法.分享给大家供大家参考,具体如下: 其实原理很简单,用到的是selector,用来设置android:background和an ...

  3. Android的圆角按钮和按钮颜色

    1. android 设置圆角按钮后,按下按钮后,还能改变按钮的颜色 <span style="font-size:18px;"><?xml version=&q ...

  4. android studio run按钮为灰色

    文链:解决"No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-andr ...

  5. jays+android耳机,android – 响应多按钮有线耳机

    我正在驾驶一对JAYS四耳机(有线遥控器/麦克风上有3个按钮)插入Galaxy Nexus(ICS 4.0.2)进行实验. 我的问题是,只有中间的按钮被我写的测试应用程序'识别',即按键时触发了Int ...

  6. android按钮周围阴影,Android 上的按钮填充和阴影

    Android 上的按钮填充和阴影Button Padding and Shadows on Android 07/10/2018 本文内容 此 Android 平台特定控制按钮是否 :::no-lo ...

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

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

  8. Android之Button按钮

    android中的按钮控件,直接继承了TextView.,在页面上的显示是一个矩形的图形.控件的基本属性: android:id="":按钮的唯一标识. android:layou ...

  9. Android Material Design按钮样式设计

    Today we'll dive deep into Android Buttons in Material Design and develop an application that showca ...

最新文章

  1. 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字
  2. boost::geometry::topological_dimension用法的测试程序
  3. linux怎么设置tomcat自动启动,linux添加tomcat服务并设置开机启动
  4. MySQL 学习三:来教你如何完全卸载掉本地“头大的” MySQL 数据库!
  5. ad域需要自建dns服务器吗,创建AD DS域服务(图文详解)
  6. spl_autoload_register 注册自己的自动加载函数(__autoload())
  7. 动态修改css 规则
  8. grads插值_Grads画等值线(一)-----心得感言
  9. 基于FPGA的SDRAM控制器设计(一)
  10. flash插件android 6.0.1,flash player 10.1
  11. 算法题:矩阵修改为黑白矩阵
  12. 电脑如何显示文件后缀名
  13. 自建魔兽世界sf服务器,魔兽世界80自己搭服务器,热度却持续不到一天,全部用命令!...
  14. 【机器学习】10:朴素贝叶斯做文本分类
  15. 容器 java 时区_docker容器修改时区(java应用log信息与标准容器时间有八个小时时间差)...
  16. 关于unity debug.log日志不出现的问题
  17. 常用图形渲染API简介
  18. resnet50结构图
  19. 02.创新与企业精神——有目的的创新和创新机遇的7个来源
  20. OpenSSL 制作证书时出现的错误的解决办法

热门文章

  1. 无法找到脚本文件 C:/Windows/explorer.exe:574323188.vbs
  2. angular中的MVVM模式
  3. [转载] Python学习笔记 String类型常用方法
  4. Vue.js 学习笔记 一
  5. html前端通过canvas生成验证码
  6. django中的Q查询
  7. 详解python 字符串
  8. 实训|第七天横扫Linux磁盘分区、软件安装障碍附制作软件仓库
  9. RPM方式安装MySQL5.5.48 (Aliyun CentOS 7.0 卸载MySQL5.7)
  10. OpenStack点滴01-概览