Button的使用
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class button extends AppCompatActivity {
private TextView tv_res;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.button);
tv_res= findViewById(R.id.text);
}
public void onButtonClick(View view){
tv_res.setText("点击按钮我就会出现");
}
}
<?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:id="@+id/show"
tools:ignore="ExtraText">
<!-- android:id="@+id/btn_confirm"-->
<!-- android:layout_width="wrap_content" 或 match_parent-->
<!-- android:layout_height="48dp" 推荐固定高度-->
<!-- android:text="确认" 按钮文字-->
<!-- android:textColor="#FFFFFF" 文字颜色 -->
<!-- android:background="@color/black" 背景选择器 -->
<!-- android:onClick="onButtonClick" XML绑定点击事件(不推荐生产环境使用)-->
<!-- android:enabled="true" 是否可点击-->
<!-- android:clickable="true" 明确声明可点击(默认true)-->
<!-- android:focusable="true" 允许获取焦点-->
<Button
android:id="@+id/btn_confirm"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:text="确认"
android:textColor="#FFFFFF"
android:background="@color/black"
android:onClick="onButtonClick"
android:enabled="true"
android:clickable="true"
android:focusable="true" />
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/text"
android:text="" />
</LinearLayout>
Button设置图片和文字同时出现
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--图片在左边-->
<Button
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="drawableLeft"
android:textSize="40dp"
android:drawableLeft="@drawable/好评A"/>
<!--图片在右边-->
<Button
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="drawableRight"
android:textSize="40dp"
android:drawableRight="@drawable/好评A"/>
<!--图片在上边-->
<Button
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="drawableTop"
android:textSize="40dp"
android:drawableTop="@drawable/好评A"/>
<!--图片在下边-->
<Button
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="drawableBottom"
android:textSize="40dp"
android:drawableBottom="@drawable/好评A"/>
</LinearLayout>

点击事件
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class buttonActivity extends AppCompatActivity implements View.OnClickListener{
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.button_activity);
Button btn1 = findViewById(R.id.btn1);
Button btn2 = findViewById(R.id.btn2);
text = findViewById(R.id.text);
//添加事件监听
//1.重写监听方法的添加方式
btn1.setOnClickListener(new MyOnClickListener(text));
//2.自带的添加方式
btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.btn2){
text.setText("点击了btn2");
}
}
// 自定义类
static class MyOnClickListener implements View.OnClickListener{
private final TextView text;
//点击按钮后获取到文本的值
public MyOnClickListener(TextView text){
this.text = text;
}
@Override
//点击按钮后具体的事件处理程序
public void onClick(View v) {
String attr = v.toString();
text.setText("点击了调用我的按钮"+attr);
}
}
}
<?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">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="按钮1" />
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="按钮2" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="hello"/>
</LinearLayout>
长按点击事件
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class longClickActivity extends AppCompatActivity{
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.button_activity);
Button btn1 = findViewById(R.id.btn1);
text = findViewById(R.id.text);
//长按大概一秒事件触发
btn1.setOnLongClickListener(v->{
text.setText("长按点击事件");
return true;
});
}
}