小伙伴们确定都有在玩儿一些游戏,好比和平精英,在界面上展现的名字,其实就是Android中就是我们上一篇介绍到的TextView控件。而今天,咱们再给你们带来一个很是好玩儿的控件,这个控件能够结合我们以前的讲到的内容作不少有趣的事情~php
public class Button extends TextViewjava
java.lang.Object ↳ android.view.View ↳ android.widget.TextView ↳ android.widget.Button 已知直接子类 CompoundButton 已知间接子类 CheckBox, RadioButton, Switch, ToggleButtonandroid
Button是Android中一个很是简单的控件,在咱们平时的项目中,能够说是很是的常见,使用率也是至关高。用户能够按下或单击按钮来执行操做。ide
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">
<Button android:id="@+id/btn_clickMe" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是一个按钮,快点我" />
<ImageButton android:id="@+id/btn_clickImg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" />
</LinearLayout>
复制代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnClickMe = (Button) findViewById(R.id.btn_clickMe);
btnClickMe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Perform action on click
Toast.makeText(MainActivity.this, "已点中", Toast.LENGTH_SHORT).show();
}
});
ImageButton btnClickImg = (ImageButton) findViewById(R.id.btn_clickImg);
btnClickImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Perform action on click
Toast.makeText(MainActivity.this, "已点中图片", Toast.LENGTH_SHORT).show();
}
});
}
}
复制代码
你还能够经过实现View.OnClickListener
接口并重写onClick
方法,来设置多个点击事件布局
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_clickMe).setOnClickListener(this);
findViewById(R.id.btn_clickImg).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_clickMe:
Toast.makeText(MainActivity.this, "已点中", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_clickImg:
Toast.makeText(MainActivity.this, "已点中图片", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
复制代码
可是,您还可使用android:onClick
属性为XML
布局中的按钮分配一个方法,而不是对Activity中对按钮实现onClickListener。例如:this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="clickButton" android:text="我是一个按钮,快点我" />
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" android:onClick="clickImg" />
</LinearLayout>
复制代码
如今,当用户点击按钮时,Android系统会调用Activity的自定义(视图)方法。此方法必须是公共的,而且接受一个视图做为它的惟一参数。例如:spa
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void clickButton(View view) {
Toast.makeText(MainActivity.this, "已点中", Toast.LENGTH_SHORT).show();
}
public void clickImg(View view) {
Toast.makeText(MainActivity.this, "已点中图片", Toast.LENGTH_SHORT).show();
}
}
复制代码
最终效果以下:设计
每一个按钮都使用系统的默认按钮背景进行样式化,若是您对默认按钮样式不满意,而且但愿对其进行自定义以匹配应用程序的设计,那么您能够用可绘制的状态列表替换按钮的背景图像。状态列表可绘制是在XML中定义的可绘制资源,它根据按钮的当前状态更改其图像。一旦定义了一个能够用XML绘制的状态列表,就能够将它应用到具备android:background
属性的按钮上。3d
1.设置背景图code
方法一:在xml布局里直接设置背景图
方法二:在Java代码里给button
设置背景图(setBackground
和setBackgroundResource
两种任意一种便可)
btnClickMe.setBackground(ContextCompat.getDrawable(this, R.mipmap.ic_launcher));
btnClickMe.setBackgroundResource(R.mipmap.ic_launcher);
复制代码
2.设置背景色
方法一:在xml布局里直接设置背景色
方法二:在Java代码里给button
设置背景色(setBackgroundColor
中如下两种方法任意一种便可)
btnClickMe.setBackgroundColor(getResources().getColor(R.color.colorAccent));
btnClickMe.setBackgroundColor(Color.parseColor("#ff0000"));
复制代码
3.经过shape
设置背景样式
在drawable包下新建xml,我这里命名为bg_btn_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/colorAccent" />
<corners android:bottomLeftRadius="10dp" android:bottomRightRadius="50dp" android:topLeftRadius="10dp" android:topRightRadius="50dp" />
<stroke android:width="3dp" android:color="#99CCFF" />
</shape>
复制代码
android:shape="rectangle"
//样式为矩形(四个属性:rectangle
矩形、oval
椭圆形、line
线性形状、ring
环形)solid
:指定内部填充色corners
:定义圆角 (radius所有的圆角半径 、bottomLeftRadius
左下角的圆角半径 、bottomRightRadius
右上角的圆角半径 、topLeftRadius
左上角的圆角半径 、topRightRadius
右上角的圆角半径 )stroke
:描边属性,能够定义描边的宽度,颜色,虚实线等(width
描边的宽度、color
描边的颜色)方法一:在xml布局里直接设置自定义shape
背景,若是想给按钮内部文字设置边距还能够本身设置padding
方法二:在Java代码里给button
设置自定义shape
背景(setBackground
和setBackgroundResource
两种任意一种便可)
btnClickMe.setBackground(ContextCompat.getDrawable(this, R.drawable.bg_btn));
btnClickMe.setBackgroundResource(R.drawable.bg_btn);
复制代码
此时,若是还想要仿iOS同样的按钮点击按下和松开效果,那么能够这样
再在drawable包下新建一个xml,我这里命名为bg_btn_press.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/colorPrimary" />
<corners android:bottomLeftRadius="10dp" android:bottomRightRadius="50dp" android:topLeftRadius="10dp" android:topRightRadius="50dp" />
<stroke android:width="3dp" android:color="#99CCFF" />
</shape>
复制代码
而后再新建一个xml,一样是在drawable包下,使用<selector../>
元素定义了一个StateListDrawable
对象,我这里命名为bg_btn.xml
。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/bg_btn_press" android:state_pressed="true" />
<item android:drawable="@drawable/bg_btn_normal" android:state_focused="true" />
<item android:drawable="@drawable/bg_btn_normal" />
</selector>
复制代码
4.设置显示隐藏
方法一:在xml布局里直接设置
android:visibility="invisible"
复制代码
方法二:在Java代码里给button
设置
btnClickMe.setVisibility(View.VISIBLE);
复制代码
下面有清楚的布局讲述
其中visibility
还有三种属性
VISIBLE
(view可见,占据屏幕区域)、INVISIBLE
(view不可见,占据屏幕区域)、GONE
(view不可见,不占屏幕空间,原先占有的区域被其余view占据,原先布局会发生变化)。5.给button设置某一边设置图片
方法一:在xml布局里直接设置
方法二:在Java代码里给button
设置
Button btnClickMe = findViewById(R.id.btn_clickMe);
//代码设置drawableLeft资源图片
Drawable drawableLeft = getResources().getDrawable(R.mipmap.ic_launcher);
btnClickMe.setCompoundDrawablesWithIntrinsicBounds(drawableLeft, null, null, null);
//代码设置drawable和view之间的距离
btnClickMe.setCompoundDrawablePadding(4);
复制代码
drawableLeft
左图标drawableRight
右图标drawableTop
上图标drawableBottom
下图标drawablePadding
图标与文字的间距以上就是Android
中最经常使用的UI控件Button
的介绍,在咱们的实际开发中,好多控件都拥有onClick()事件,那么上一篇文章的TextView有没有,就要靠小伙伴们本身去尝试了~
PS:若是还有未看懂的小伙伴,欢迎加入咱们的QQ技术交流群:892271582,里面有各类大神回答小伙伴们遇到的问题哦~