按钮控件Button由TextView派生而来,它们之间的区别有:
Button拥有默认的按钮背景,而TextView默认无背景;
Button的内部文本默认居中对齐,而TextView的内部文本默认靠左对齐;
Button会默认将英文字母转为大写,而TextView保持原始的英文大小写;
textAllCaps属性,它指定了是否将英文字母转为大写,为true是表示自动转为大写,为false表示不做大写转换。
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下面的英文默认大写"
android:textColor="@color/black"
android:textSize="17sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textColor="@color/black"
android:textAllCaps="false"
android:textSize="17sp"/>
onClick属性,它用来接管用户的点击动作,指定了点击按钮时要触发哪个方法;
package com.example.study01.util;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtil {
public static String getNowTime(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
return simpleDateFormat.format(new Date());
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ButtonStyleActivity"
android:orientation="vertical">
<TextView
android:id="@+id/tv_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击下方按钮获取当前时间"
android:textColor="@color/black"
android:textSize="17sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textColor="@color/black"
android:textAllCaps="false"
android:textSize="17sp"
android:onClick="doClick"/>
LinearLayout>
package com.example.study01;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.study01.util.DateUtil;
public class ButtonStyleActivity extends AppCompatActivity {
private TextView tv_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_style);
tv_id = findViewById(R.id.tv_id);
}
public void doClick(View view) {
String desc = String.format("%s 您点击了按钮: %s",DateUtil.getNowTime(),((Button)view).getText());
tv_id.setText(desc);
}
}
监听器,意思是专门监听控件的动作行为。只有控件发生了指定的动作,监听器才会触发开关去执行对应的代码逻辑。
按钮控件有两种常用的监听器:
点击监听器,通过setOnClickListener方法设置。按钮被按住少于500毫秒时,会触发点击事件。
长按监听器,通过setOnLongClickListener方法设置。按钮被按住超过500毫秒时,会触发长按事件。
package com.example.study01;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.study01.util.DateUtil;
public class ButtonClickActivity extends AppCompatActivity implements View.OnClickListener{
private static TextView tv_btn_time1;
private TextView tv_btn_time2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_click);
Button btn_click1 = findViewById(R.id.btn_click1);
tv_btn_time1 = findViewById(R.id.tv_btn_time1);
btn_click1.setOnClickListener(new MyOnClickListener());
Button btn_click2 = findViewById(R.id.btn_click2);
btn_click2.setOnClickListener(this);
tv_btn_time2 = findViewById(R.id.tv_btn_time2);
}
@Override
public void onClick(View v) {
String desc = String.format("%s 您点击了按钮: %s", DateUtil.getNowTime(),((Button)v).getText());
tv_btn_time2.setText(desc);
}
/*这里使用静态类防止内存泄漏*/
static class MyOnClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
String desc = String.format("%s 您点击了按钮: %s", DateUtil.getNowTime(),((Button)v).getText());
tv_btn_time1.setText(desc);
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ButtonClickActivity"
android:orientation="vertical">
<Button
android:id="@+id/btn_click1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="获取当前时间1"/>
<TextView
android:id="@+id/tv_btn_time1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""/>
<Button
android:id="@+id/btn_click2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="获取当前时间2"/>
<TextView
android:id="@+id/tv_btn_time2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""/>
LinearLayout>
package com.example.study01;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.study01.util.DateUtil;
public class ButtonLongClickActivity extends AppCompatActivity {
private TextView tv_long;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_long_click);
tv_long = findViewById(R.id.tv_long);
Button btn_long_click = findViewById(R.id.btn_long_click);
btn_long_click.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
String desc = String.format("%s 您点击了按钮: %s", DateUtil.getNowTime(),((Button)v).getText());
tv_long.setText(desc);
return true;
}
});
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ButtonLongClickActivity"
android:orientation="vertical">
<Button
android:id="@+id/btn_long_click"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="长按点击事件"/>
<TextView
android:id="@+id/tv_long"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="17sp"/>
LinearLayout>
图像视图展示的图片通常位于res/drawable***目录,设置图像视图的显示图片有两种方
式:
在XML文件中,通过属性android:src设置图片资源,属性值格式形如“@drawable/不含扩展名的图片名称”
在Java代码中,调用setImageResource方法设置图片资源,方法参数格式形如“R.drawable.不含扩展名的图片名称”
ImageButton是显示图片的图像按钮,但它继承自ImageView,而非继承Button。
ImageButton和Button之间的区别有:
Button既可显示文本也可显示图片,ImageButton只能显示图片不能显示文本。
ImageButton上的图像可按比例缩放,而Button通过背景设置的图像会拉伸变形。
Button只能靠背景显示一张图片,而ImageButton可分别在前景和背景显示图片,从而实现两张图片叠加的效果。