Button控件
-
文字大小,颜色
-
自定义背景形状
-
自定义按压效果
-
点击事件
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="15dp">
<Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FB1707"
android:text="按钮1"
android:textColor="#FFFFFF"
android:textSize="24sp" />
<Button
android:id="@+id/btn_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btn_1"
android:layout_marginTop="10dp"
android:background="@drawable/bg_btn2"
android:text="按钮2"
android:textColor="#000000"
android:textSize="24sp" />
<Button
android:id="@+id/btn_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btn_2"
android:layout_marginTop="10dp"
android:background="@drawable/bg_btn3"
android:text="按钮3"
android:textColor="#000000"
android:textSize="24sp" />
<Button
android:id="@+id/btn_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btn_3"
android:layout_marginTop="10dp"
android:background="@drawable/bg_btn4"
android:text="按钮4"
android:onClick="showToast "
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="OnClick" />
<TextView
android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="文字1"
android:textSize="24sp"
android:textColor="#000000"
android:background="@drawable/bg_btn2"
android:layout_below="@id/btn_4"
android:gravity="center"/>
</RelativeLayout>
java:
package com.example.androidprojiect04;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class ButtonActivity extends AppCompatActivity {
private Button mbtn3;
private TextView mtv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
mbtn3 = findViewById(R.id.btn_3);
mbtn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(ButtonActivity.this,"11111111",Toast.LENGTH_LONG).show();
}
});
mtv1 = findViewById(R.id.tv_1);
mtv1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(ButtonActivity.this,"Tv1",Toast.LENGTH_LONG).show();
}
});
}
public void showToast(View view){
Toast.makeText(this,"11111111",Toast.LENGTH_LONG).show();
}
}