一. 继承图

二. Xml方式
继承自 TextView 的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| android:text android:textColor android:textSize android:textStyle android:textAllCape
android:background
android:drawableLeft android:drawablePadding
android:hint android:textColorHint
android:inputType
android:ellipsize
android:maxLength
android:minLines android:maxLines
android:lines android:lineSpacingExtra
android:autoLink
android:textIsSelectable
android:shadowRadius android:shadowColor android:shadowDx android:shadowDy
android:clickable android:focusable android:enabled
android:cursorVisible android:textCursorDrawable
android:onClick
|
三. Onclick 的使用方法
1. xml中的onClick
1 2 3 4 5
| android:onClick="show"
public void show(View view){ }
|
2. 匿名类的方式
单个类需要监听者
1 2 3 4 5 6 7 8
| Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } });
|
3. 实现OnClickListener接口中的onClick方法
多个类需要监听者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public class SecondActivity extends AppCompatActivity implements View.OnClickListener {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second);
Button btn1 = findViewById(R.id.btn1); btn1.setOnClickListener(this); Button btn2 = findViewById(R.id.btn2); btn2.setOnClickListener(this); }
@Override public void onClick(View v) { if(v.getId() == R.id.btn1){ }else if(v.getId() == R.id.btn2){ } } }
|
4. 创建内部类的方式
不常用,麻烦
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public class SecondActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second);
Button btn = findViewById(R.id.btn); btn.setOnClickListener(new MyButton); }
}
private class MyButton implements View.OnClickListener { @Override public void onClick(View v) { } }
|
5. Lambda表达式
难理解
1 2 3 4 5 6 7 8 9 10 11
| Button btn = findViewById(R.id.btn);
button.setOnClickListener((v) -> { });
button.setOnClickListener(v -> { });
|
四. 常见的问题
在 Android 5.0 推出之后,点击 Button 默认增加了水波纹的动画效果。但是按照往常的方式给 Button 设置了 background 之后,发现水波纹效果没有了。
正确的改变 Button 颜色的姿势如下:
首先在 values/styles.xml 文件中添加如下风格:
1 2 3
| <style name="BlueButtonStyle" parent="ThemeOverlay.AppCompat"> <item name="colorButtonNormal">@android:color/holo_blue_light</item> </style>
|
或者是:
1 2 3
| <style name="RedButtonStyle" parent="Widget.AppCompat.Button.Borderless"> <item name="colorButtonNormal">@android:color/holo_red_light</item> </style>
|
这两种主题都可以,尝试之后,发现效果一致。
在 xml 文件中使用:
1 2 3 4 5 6 7 8
| <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" android:text="Button" android:textAllCaps="false" android:textColor="@android:color/white" android:theme="@style/RedButtonStyle"/>
|
参考文章
解决 Button 设置 background 之后点击动画效果消失的问题
Lambda表达式的使用实例(转载)
安卓按钮4种点击事件
Android控件之Button介绍