Android消息提示框及CheckBox组件

目录

一、Toast(消息提示框)

1、创建显示普通文本的Toast

2、创建显示带图片的Toast 

二、CheckBox组件

1、在CheckBox在XML文件中的基本语法

2、CheckBox 选中或未选中状态的设置方法


一、Toast(消息提示框)

Toast是Android系统中非常有用的消息提示组件。该组件可用于在屏幕中显示一消息提示框,该消息提示框没有任何控制按钮,并不会获得焦点,经过一定时间后自动消失,因此,在用户使用Android应用程序时,常用Toast来显示快速的提示信息。

Toast组件的使用只需要以下4个步骤即可实现:

1、调用Toast.makeText()方法来创建一个Toast对象

2、设置调用Toast对象的成员函数来设置Toast的显示位置和显示内容

3、如果需要自定义Toast样式,只需创建对应的View组件,并通过Toast中的setView函数来显示用户自定义的视图布局

4、调用Toast的show()函数显示消息提示框

1、创建显示普通文本的Toast

利用Toast.makeText()方法创建一个Toast对象,然后显示Toast信息,Java代码如下:

// Toast.makeText参数注释
// 第1个参数:当前的上下文环境,可用getApplicationContext()或this
// 第2个参数:要显示的字符串。也可使R.string中的字符串ID
// 第3个参数:显示的时间长短,Toast默认的参数有LENGTH_LONG(长)和LENGTH_SHORT(短)
// 可以使用毫秒,如2000ms
Toast toast = Toast.makeText(getApplicationContext(),"默认的Toast",Toast.LENGTH_SHORT);
// 显示Toast信息
toast.show();

2、创建显示带图片的Toast 

首先调用 Toast.makeText()方法来创建一个Toast对象,然后,通过Toast的 getView()获取Toast视图,并创建一个线性布局和图像组件的视图,之后把Toast和ImageView 加入到线性布局中组成一个新的视图,最后显示Toast信息。Java 程序代码如下:

Toast toast=Toast.makeText(getApplicationContext(),"显示带图片的 toast", 3000);toast.setGravity (Gravity. CENTER, 0, 0);
//创建图片视图对象
ImageView imageView= new ImageView (getApplicationContext () ) ;
//设置图片
imageView.setImageResource (R.drawable.ic_launcher) ;
//获得Toast的布局
LinearLayout toastView= (LinearLayout) toast.getView();
//设置此布局为横向的
toastView.setorientation (LinearLayout. HORIZONTAL) ;
//将 ImageView加入到此布局中的第1个位置
toastView.addView (imageView, 0);
toast.show () ;

二、CheckBox组件

在Android中,CheckBox组件被称为多选按钮,该组件可允许在一组选项中进行单选或多选,如兴趣爱好、课程等不同选项的选择

1、在CheckBox在XML文件中的基本语法

格式如下:

<CheckBox
    属性列表
/>

2、CheckBox 选中或未选中状态的设置方法

在CheckBox组件中有一个checked属性,它可用于判断CheckBox是否被选中。一般改变checked属性有以下3种方法。

① 在XML文件中,通过android:checked设置,当取值为true时表示选中,当取值为false时表示未选中。
② 在Java程序代码中,通过 setChecked(boolean)设置CheckBox是否被选中,还可以利用isChecked()方法来判断组件状态是否被选中。
③ 通过用户触摸改变选中状态。当CheckBox组件状态发生改变时,将会触发OnCheckedChange 响应事件,因此,我们可以通过 OnCheckedChangeListener方法设置事件监听器,以实现对CheckBox状态变化的监控,并根据程序设计的不同要求重写OnCheckedChange响应事件中的代码。

CheckBox组件设置兴趣爱好示例:

布局文件中TextView、CheckBox组件设置的代码如下:

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="66dp"
        android:layout_height="55dp"
        android:text="游泳"
        app:layout_constraintBottom_toBottomOf="@+id/textView2"
        app:layout_constraintStart_toEndOf="@+id/textView2"
        app:layout_constraintTop_toTopOf="@+id/textView2" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="86dp"
        android:layout_height="55dp"
        android:text="打篮球"
        app:layout_constraintBottom_toBottomOf="@+id/textView2"
        app:layout_constraintStart_toEndOf="@+id/checkBox"
        app:layout_constraintTop_toTopOf="@+id/textView2" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="66dp"
        android:layout_height="55dp"
        android:layout_marginEnd="84dp"
        android:text="唱歌"
        app:layout_constraintBottom_toBottomOf="@+id/textView2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/checkBox2"
        app:layout_constraintTop_toTopOf="@+id/textView2" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="91dp"
        android:layout_height="44dp"
        android:layout_marginStart="52dp"
        android:text="爱好"
        android:textSize="28dp"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.365" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="306dp"
        android:layout_height="116dp"
        android:text="TextView"
        android:textSize="25dp"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.495"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.546" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="132dp"
        android:text="提交"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/textView3"
        app:layout_constraintStart_toStartOf="@+id/textView3" />

在MainActivity.java程序中编写CheckBox选项状态变换时的处理程序:

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import java.util.Hashtable;
import java.util.Map;

public class MainActivity4 extends AppCompatActivity {


    TextView tv2;
    CheckBox cb1,cb2,cb3;
    Button bt;
    Map<String,String> map = new Hashtable<String,String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);

        //控件实例化,关联界面上的控件
        tv2 = findViewById(R.id.textView3);
        cb1 = findViewById(R.id.checkBox);
        cb2 = findViewById(R.id.checkBox2);
        cb3 = findViewById(R.id.checkBox3);
        bt = findViewById(R.id.button);

        //实现监听复选框的控件
        cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if(isChecked){
                    map.put("yy",cb1.getText().toString());
                    tv2.setText("您选中了" + cb1.getText().toString());
                } else {
                    map.remove("yy");
                }
            }
        });
        cb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if(isChecked){
                    map.put("dlq",cb2.getText().toString());
                    tv2.setText("您选中了" + cb2.getText().toString());
                } else {
                    map.remove("dlq");
                }
            }
        });
        cb3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if(isChecked){
                    map.put("cg",cb3.getText().toString());
                    tv2.setText("您选中了" + cb3.getText().toString());
                } else {
                    map.remove("cg");
                }
            }
        });

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv2.setText(show());
            }
        });
    }

    public String show(){
        StringBuilder sb = new StringBuilder();
        String st = "你的爱好是:";
        if(map.size() == 0){
            return "你没选择任何爱好";
        } else {
            sb.append(st);
            for(String key : map.keySet()){
                sb.append(map.get(key));
            }
        }
        return sb.toString();
    }
}

运行效果:

2cb9cb52b15b4c7499a9eb417fc78cb6.gif

猜你喜欢

转载自blog.csdn.net/m0_61961937/article/details/127498450