目录
4、按钮触控
按钮控件Button
按钮控件Button由TextView派生而来,它们之间的区别有:
(1)Button拥有默认的按钮背景,而TextView默认无背景;
(2)Button的内部文本默认居中对齐,而TextView的内部文本默认靠左对齐;
(3)Button会默认将英文字母转为大写,而TextView保持原始的英文大小写;
按钮控件的新增属性:
与TextView相比,Button增加了两个新属性: (1)textAllCaps属性,它指定了是否将英文字母转为大写,为true是表示自动转为大写,为false表示不做大写转换。 (2)onClick属性,它用来接管用户的点击动作,指定了点击按钮时要触发哪个方法;
package com.example.chapter03;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class BottonStyleActivity extends AppCompatActivity {
private TextView tv_result;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_style);
tv_result=findViewById(R.id.tv_result);
}
public void doClick(View view){
String desc=String.format("%s 您点击了按钮:%s",DateUtil.GetNowTime(),((Button)view).getText());
tv_result.setText(desc);
}
}
package com.example.chapter03.utils;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtil {
public static String GetNowTime(){
SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
return sdf.format(new Date());
}
}
<?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"
android:padding="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下面的按钮英文默认大写"
android:textColor="@color/black"
android:textSize="17dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="HelloWorld"
android:textColor="@color/black"
android:textSize="17dp"
android:textAlignment="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下面的按钮英文保持原状"
android:textColor="@color/black"
android:textSize="17dp"
android:textAlignment="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="helloworld"
android:textColor="@color/black"
android:textSize="17dp"
android:textAllCaps="false"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="直接指定点击方法"
android:textColor="@color/black"
android:textSize="17dp"
android:textAllCaps="false"
android:onClick="doClick"
/>
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这里查看按钮的点击结果"
android:textColor="@color/black"
android:textSize="17dp"
android:textAlignment="center"/>
</LinearLayout>
点击事件和长按事件
监听器,意思是专门监听控件的动作行为。只有控件发生了指定的动作,监听器才会触发开关去执行对应的代码逻辑。 按钮控件有两种常用的监听器:
(1)点击监听器,通过setOnClickListener方法设置。按钮被按住少于500毫秒时,会触发点击事件。
(2)长按监听器,通过setOnLongClickListener方法设置。按钮被按住超过500毫秒时,会触发长按事件。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_longclick_single"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="指定单独的长按监听器"
android:textColor="#000000"
android:textSize="15sp" />
<Button
android:id="@+id/btn_longclick_public"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="指定公共的长按监听器"
android:textColor="#000000"
android:textSize="15sp" />
</LinearLayout>
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="这里查看按钮的长按结果"
android:textColor="#000000"
android:textSize="15sp" />
</LinearLayout>
package com.example.chapter03;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.chapter03.utils.DateUtil;
public class ButtonLongClickActivity extends AppCompatActivity implements View.OnLongClickListener {
private TextView tv_result; // 声明一个文本视图实例
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.active_button_long_click);
tv_result = findViewById(R.id.tv_result); // 获取名叫tv_result的文本视图
// 从布局文件中获取名叫btn_click_single的按钮控件
Button btn_longclick_single = findViewById(R.id.btn_longclick_single);
// 设置长按监听器,一旦用户长按按钮,就触发监听器的onLongClick方法
btn_longclick_single.setOnLongClickListener(new MyOnLongClickListener());
// 从布局文件中获取名叫btn_click_public的按钮控件
Button btn_longclick_public = findViewById(R.id.btn_longclick_public);
// 设置长按监听器,一旦用户长按按钮,就触发监听器的onLongClick方法
btn_longclick_public.setOnLongClickListener(this);
}
@Override
public boolean onLongClick(View v) { // 长按事件的处理方法
if (v.getId() == R.id.btn_longclick_public) { // 来自于按钮btn_longclick_public
String desc = String.format("%s 您长按了按钮:%s",
DateUtil.GetNowTime(), ((Button) v).getText());
tv_result.setText(desc); // 设置文本视图的文本内容
}
return true;
}
// 定义一个长按监听器,它实现了接口View.OnLongClickListener
class MyOnLongClickListener implements View.OnLongClickListener {
@Override
public boolean onLongClick(View v) { // 长按事件的处理方法
String desc = String.format("%s 您长按了按钮:%s",
DateUtil.GetNowTime(), ((Button) v).getText());
tv_result.setText(desc); // 设置文本视图的文本内容
return true;
}
}
}
禁用与恢复按钮
在实际业务中,按钮通常拥有两种状态,即不可用状态与可用状态,它们在外观和功能上的区别如下:
(1)不可用按钮:按钮不允许点击,即使点击也没反应,同时按钮文字为灰色;
(2)可用按钮:按钮允许点击,点击按钮会触发点击事件,同时按钮文字为正常的黑色; 是否允许点击由enabled属性控制,属性值为true时表示允许点击,为false时表示不允许点击。
package com.example.chapter03;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.chapter03.utils.DateUtil;
public class ButtonEnableActivity extends AppCompatActivity {
private TextView tv_result; // 声明一个文本视图实例
private Button btn_test; // 声明一个按钮控件实例
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.active_button_enable);
tv_result = findViewById(R.id.tv_result); // 因为按钮控件的setOnClickListener方法来源于View基类,所以也可对findViewById得到的视图直接设置点击监听器
findViewById(R.id.btn_enable).setOnClickListener(this::onClick);
findViewById(R.id.btn_disable).setOnClickListener(this::onClick);
btn_test = findViewById(R.id.btn_test); // 获取名叫btn_test的按钮控件
btn_test.setOnClickListener(this::onClick); // 设置btn_test的点击监听器
}
public void onClick(View v) {
if (v.getId() == R.id.btn_enable) {
btn_test.setTextColor(Color.BLACK); // 设置按钮的文字颜色
btn_test.setEnabled(true); // 启用当前控件
} else if (v.getId() == R.id.btn_disable) { // 点击了按钮“禁用测试按钮”
btn_test.setTextColor(Color.GRAY); // 设置按钮的文字颜色
btn_test.setEnabled(false); // 禁用当前控件
} else if (v.getId() == R.id.btn_test) { // 点击了按钮“测试按钮”
String desc = String.format("%s 您点击了按钮:%s",
DateUtil.GetNowTime(), ((Button) v).getText());
tv_result.setText(desc); // 设置文本视图的文本内容
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_enable"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="启用测试按钮"
android:textColor="#000000"
android:textSize="17sp"
android:onClick="onClick"
/>
<Button
android:id="@+id/btn_disable"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="禁用测试按钮"
android:textColor="#000000"
android:textSize="17sp"
android:onClick="onClick"/>
</LinearLayout>
<Button
android:id="@+id/btn_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:text="测试按钮"
android:textColor="#888888"
android:textSize="17sp"
android:onClick="onClick"/>
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="这里查看测试按钮的点击结果"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="17sp" />
</LinearLayout>
4、图像显示
图像视图ImageView
(1)在XML文件中,通过属性android:src设置图片资源,属性值格式形如“@drawable/不含扩展名的图片名称”。
(2)在Java代码中,调用setImageResource方法设置图片资源,方法参数格式形如“R.drawable.不含扩展名的图片名称”。
package com.example.chapter03; import android.os.Bundle; import android.widget.ImageView; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; public class ImageScaleActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.active_image_scale); ImageView iv_scale=findViewById(R.id.iv_scale); //iv_scale.setImageResource(R.drawable.ars1);//更换图片 iv_scale.setScaleType(ImageView.ScaleType.CENTER_CROP); } }
<?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">
<ImageView
android:id="@+id/iv_scale"
android:layout_width="match_parent"
android:layout_height="220dp"
android:layout_marginTop="30dp"
android:src="@drawable/beautifulwomen"
android:scaleType="fitStart"
/>
</LinearLayout>
图像按钮ImageButton
ImageButton是显示图片的图像按钮,但它继承自ImageView,而非继承Button。 ImageButton和Button之间的区别有:
(1)Button既可显示文本也可显示图片,ImageButton只能显示图片不能显示文本。
(2)ImageButton上的图像可按比例缩放,而Button通过背景设置的图像会拉伸变形。 (3)Button只能靠背景显示一张图片,而ImageButton可分别在前景和背景显示图片,从而实现两张图片叠加的效果。
ImageButton与ImageView之间的区别有:
(1)ImageButton有默认的按钮背景,ImageView默认无背景。
(2)ImageButton默认的缩放类型为center,而ImageView默认的缩放类型为fitCenter。
<?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">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:src="@drawable/beautifulwomen" />
</LinearLayout>
同时展示文本与图像
同时展示文本与图像的可能途径包括:
(1)利用LinearLayout对ImageView和TextView组合布局。
(2)通过按钮控件Button的drawable***属性设置文本周围的图标。
drawableTop:指定文字上方的图片。
drawableBottom:指定文字下方的图片。
drawableLeft:指定文字左边的图片。
drawableRight:指定文字右边的图片。
drawablePadding:指定图片与文字的间距。
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="图标在左"
android:drawableLeft="@drawable/ic_about"
android:drawablePadding="50dp"
/>
</LinearLayout>