Android 开发布局笔记01 控件

news2024/9/21 2:37:41

Relative Layout

前端界面代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button1"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"/>

    <Button
        android:id="@+id/btn_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="260dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button3"
        android:layout_toRightOf="@+id/btn_2"
        android:layout_alignBottom="@+id/btn_2"
        android:layout_marginBottom="100dp"
        />

</RelativeLayout>

在这里插入图片描述

LinearLayout

线性布局(LinearLayout)主要以水平或垂直方式来显示界面中的控件。当控件水平排列时,显示顺序依次为从左到右,当控件垂直排列时,显示顺序依次为从上到下。
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button_01"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button_02"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button_03"/>


</LinearLayout>

在这里插入图片描述

TableLayout 布局

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:stretchColumns="2"
    >

    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button01"
            />
    </TableRow>
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button02"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button022"
            />
    </TableRow>

    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button03"
            android:layout_column="2"
            />

    </TableRow>
</TableLayout>

在这里插入图片描述

简单控件

控件 : 页面的组成的主要元素,用于 和 用户进行交互。

控件分类

在这里插入图片描述
控件属性
在这里插入图片描述
控件布局

在这里插入图片描述

TextView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView显示的文本信息"
        android:textSize="30sp"
        android:textColor="#FF5722"
        android:textStyle="italic"
        android:layout_centerHorizontal="true"
        />


</RelativeLayout>

在这里插入图片描述
设置布局的步骤
1.确定布局 Determine the layout
2.多少控件 How many controls?
3.格局 和 位置 Pattern and Locations

小案例

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">

    <TextView
        android:id="@+id/tv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="知识就像是内裤underwear"
        android:textSize="30sp"
        android:textColor="#673AB7"
        android:textStyle="bold"
        android:layout_centerHorizontal="true"
        />

    <TextView
        android:id="@+id/tv_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="看不见但是很重要"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/tv_1"
        android:layout_marginTop="100dp"
        android:textSize="30sp"
        android:textColor="#EC0A0A"
        android:textStyle="italic"
        />

</RelativeLayout>

在这里插入图片描述

Button

Buton控件继承自 TexView控件,既可以显示文本,又可以显示图片,同时允许用户通过点击来执行操作,当Button控件被点击时,被按下与弹起的背景会有个动态的切换效果,这个效果就是点击效果。
最重要的作用:响应用户的点击事件

实现方式

1 在xml文件中添加按钮控件
在这里插入图片描述
2 在对应Java文件中获取按钮
在这里插入图片描述
3 给按钮设置点击事件
在这里插入图片描述
案例

设置前端布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我美吗"
        android:textSize="30sp"
        />

</RelativeLayout>

后端逻辑

package edu.northeastern.myapplication3;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button btn;

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

        // 2. 通过id获取按钮
        btn = findViewById(R.id.btn);

        // 3. 给btn按钮设置监听事件
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 4. 监听到点击之后,要做的事情
                Toast.makeText(getApplicationContext(),"当然是你最美了",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

EditText

输入框,编辑框

属性
在这里插入图片描述

案例

布局

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="姓名: "
    android:textSize="30sp"
    android:textColor="#000"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入名字"
    android:maxLines="2"
    android:textColor="#000"
    android:textStyle="italic"
    android:textSize="30sp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="密码: "
    android:textSize="30sp"
    android:textColor="#000"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入密码"
    android:maxLines="2"
    android:textColor="#000"
    android:textStyle="italic"
    android:textSize="30sp"
    android:password="true"
    />

在这里插入图片描述

RadioButton 单选按钮

RadioButton控件继承自Button控件,表示单选按钮通过android.checked属性指定是否选中

RadioGroup 单选组合框
容纳多个 单选,组合在一起,实现单选状态

案例

前端布局

<?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:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_select"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请选择你的性别: "
        android:textSize="30sp"
        android:textColor="#673AB7"
        android:layout_margin="20dp"
        />

    <RadioGroup
        android:id="@+id/rdg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/btn_man"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:layout_marginLeft="30dp"
            />

        <RadioButton
            android:id="@+id/btn_woman"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            android:layout_marginLeft="30dp"
            />
    </RadioGroup>

    <TextView
        android:id="@+id/tv_out"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="你的性别是: "
        android:textSize="30sp"
        android:textColor="#673AB7"
        android:layout_margin="20dp"
        />

    <TextView
        android:id="@+id/tv_context"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#F44336"
        android:layout_margin="20dp"
        />


</LinearLayout>

在这里插入图片描述

后端逻辑

package edu.northeastern.myapplication3;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    RadioGroup rdg;
    RadioButton btn_man, btn_woman;
    TextView tv_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 初始化
        rdg = findViewById(R.id.rdg);
        btn_man = findViewById(R.id.btn_man);
        btn_woman = findViewById(R.id.btn_woman);
        tv_content = findViewById(R.id.tv_context);

        // 点击监听
        rdg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // 用户点击之后,我们需要执行的操作
                if (checkedId == R.id.btn_man) {
                    // 用户选择了男
                    tv_content.setText("男");
                } else {
                    tv_content.setText("女");
                }
            }
        });
    }
}

案例2
案例1看懂不用看案例2了
前端

<?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:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_select"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请选择你喜欢的娱乐方式: "
        android:textSize="30sp"
        android:textColor="#673AB7"
        android:layout_margin="20dp" />

    <RadioGroup
        android:id="@+id/rdg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="30dp">

        <RadioButton
            android:id="@+id/btn_wz"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="王者" />

        <RadioButton
            android:id="@+id/btn_cc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="吃鸡" />

        <RadioButton
            android:id="@+id/btn_dq"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="打球" />

        <RadioButton
            android:id="@+id/btn_zj"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="追剧"/>

    </RadioGroup>

    <TextView
        android:id="@+id/tv_out"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="你最喜欢的娱乐方式是: "
        android:textSize="30sp"
        android:textColor="#673AB7"
        android:layout_margin="20dp"
        />

    <TextView
        android:id="@+id/tv_context"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#F44336"
        android:layout_margin="20dp"
        />


</LinearLayout>

在这里插入图片描述
后端代码

package edu.northeastern.myapplication3;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    RadioGroup rdg;
    RadioButton btn_wz, btn_cc, btn_dq, btn_zj;
    TextView tv_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 初始化
        rdg = findViewById(R.id.rdg);
        btn_wz = findViewById(R.id.btn_wz);
        btn_cc = findViewById(R.id.btn_cc);
        btn_dq = findViewById(R.id.btn_dq);
        btn_zj = findViewById(R.id.btn_zj);
        tv_content = findViewById(R.id.tv_context);

        // 点击监听
        rdg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // 用户点击之后,我们需要执行的操作
                if (checkedId == R.id.btn_wz) {
                    // 用户选择了男
                    tv_content.setText("王者");
                } else if (checkedId == R.id.btn_cc){
                    tv_content.setText("吃鸡");
                } else if (checkedId == R.id.btn_dq){
                    tv_content.setText("打球");
                }else if (checkedId == R.id.btn_zj){
                    tv_content.setText("追剧");
                }
            }
        });
    }
}

CheckBox 用于多选

在这里插入图片描述

案例

<?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:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择你的兴趣爱好"
        android:textSize="30sp"
        android:textColor="#D67455"/>

    <CheckBox
        android:id="@+id/cb_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="羽毛球"/>
    <CheckBox
        android:id="@+id/cb_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="篮球"/>
    <CheckBox
        android:id="@+id/cb_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="乒乓球"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="你的兴趣爱好有"
        android:textSize="30sp"
        android:textColor="#D67455"/>

    <TextView
        android:id="@+id/tv_res"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:layout_marginLeft="20dp"
        />
</LinearLayout>

在这里插入图片描述

package edu.northeastern.myapplication3;

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.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private CheckBox cb_1, cb_2, cb_3;
    private TextView tv_res;
    private String hobbies;

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

        cb_1 = findViewById(R.id.cb_1);
        cb_2 = findViewById(R.id.cb_2);
        cb_3 = findViewById(R.id.cb_3);
        tv_res = findViewById(R.id.tv_res);
        hobbies = "";

        // 点击1后监听事件
        cb_1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // 获取当前多选按钮的文本形象
                String text = buttonView.getText().toString();

                if (isChecked) {
                    hobbies += text + " "; // 拼接文本
                    // 设置textView添加文本
                    tv_res.setText(hobbies);
                } else {
                    // 没选择,去除当前选项文字
                    hobbies = hobbies.replace(text, "");
                    tv_res.setText(hobbies);
                }
            }
        });

        cb_2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // 获取当前多选按钮的文本形象
                String text = buttonView.getText().toString();

                if (isChecked) {
                    hobbies += text + " "; // 拼接文本
                    // 设置textView添加文本
                    tv_res.setText(hobbies);
                } else {
                    // 没选择,去除当前选项文字
                    hobbies = hobbies.replace(text, "");
                    tv_res.setText(hobbies);
                }
            }
        });

        cb_3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // 获取当前多选按钮的文本形象
                String text = buttonView.getText().toString();

                if (isChecked) {
                    hobbies += text + ""; // 拼接文本
                    // 设置textView添加文本
                    tv_res.setText(hobbies);
                } else {
                    // 没选择,去除当前选项文字
                    hobbies = hobbies.replace(text, "");
                    tv_res.setText(hobbies);
                }
            }
        });


    }
}

Toast

Toast是Android系统提供的轻量级信息提醒机制,用于向用户提示即时消息, 它显示在应用程序界面的最上层,显示一段时间后自动消失不会打断当前操作, 也不获得焦点。
在这里插入图片描述

AlertDialog对话框

在这里插入图片描述
简单案例

在这里插入图片描述

<?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:orientation="vertical"
    tools:context=".MainActivity">
    
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击"/>

</LinearLayout>
package edu.northeastern.myapplication3;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = this.<Button>findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建构建器
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                // 设置属性
                builder.setIcon(R.mipmap.ic_launcher)
                        .setTitle("普通对话框")
                        .setMessage("是否要退出应用")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 用户点击,直接退出
                                dialog.dismiss();
                                MainActivity.this.finish();// 退出当前应用
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 关闭 对话框
                                dialog.dismiss(); // 关闭alertdiag对话框
                            }
                        })
                        .create()// 创建
                        .show(); // 展示给用户
            }
        });
    }
}

案例2
设置对话框,编辑字体大小

<?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:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="单选对话框"
        android:layout_centerHorizontal="true"
        android:textSize="20sp"
        android:textColor="#DD7250"
        android:layout_marginTop="20dp"/>

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_1"
        android:layout_centerHorizontal="true"
        android:text="点击"/>

</RelativeLayout>

在这里插入图片描述

package edu.northeastern.myapplication3;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private TextView tv;
    private Button btn;
    private int userChoice = 1; // 记录用户选择
    private int[] textSizeArray = {10, 20, 30, 50}; // 字体大小对应的数据

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = findViewById(R.id.btn);
        tv = findViewById(R.id.tv_1);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 弹出单选对话框
                // 1 构建器
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                // 2 设置属性
                 builder.setIcon(R.mipmap.ic_launcher)
                        .setTitle("单选对话框")
                        // 设置单选列表
                        /**
                         * 设置单选列表
                         * 参数1 单选列表(数组)
                         */
                        .setSingleChoiceItems(new String[]{"小号", "默认 ", "中号", "大号"}, userChoice, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 记录用户选择
                                userChoice = which; // which 是记录到的选择,传递给参数userChoice
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 修改字体大小
                                tv.setTextSize(textSizeArray[userChoice]);
                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 关闭对话框
                                dialog.dismiss();
                            }
                        })
                         .create()
                         .show();
            }
        });

    }
}

AlertDialog对话框

在这里插入图片描述

<?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:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="退出应用"/>




</LinearLayout>

在这里插入图片描述

package edu.northeastern.myapplication3;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private TextView tv;
    private Button btn;
    private int userChoice = 1; // 记录用户选择
    private int[] textSizeArray = {10, 20, 30, 50}; // 字体大小对应的数据

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = findViewById(R.id.btn);


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 监听到用户点击退出,弹出AlertDialog对话框
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setIcon(R.mipmap.ic_launcher)
                        .setTitle("普通对话框")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 用户点击确定,直接退出应用
                                dialog.dismiss();
                                MainActivity.this.finish(); // 退出应用
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 关闭alertdialog对话框
                                dialog.dismiss();
                            }
                        }).create().show();
            }
        });

    }
}

多选对话框

在这里插入图片描述

<?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:orientation="vertical"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="30sp"
        android:text="多选对话框"/>

    <Button
        android:id="@+id/btn_01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="选取兴趣爱好"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"/>

    <TextView
        android:id="@+id/tv_res"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text=""
        android:layout_centerHorizontal="true"
        android:layout_below="@id/btn_01"/>
</RelativeLayout>

在这里插入图片描述

package edu.northeastern.myapplication3;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private Button btn;
    private TextView tv_res;
    private String[] hobbies = {"旅游", "美食", "看电影", "运动"}; // 多选列表
    private boolean[] userChoice = {false, false, false, false}; // userChoice 用户选择的选项
    private String text = new String();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = findViewById(R.id.btn_01);
        tv_res = findViewById(R.id.tv_res);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setIcon(R.mipmap.ic_launcher)
                        .setTitle("多选对话框")
                        /**
                         * 设置单选对话框
                         * 参数1 多选列表(数组)
                         *
                         */
                        .setMultiChoiceItems(hobbies, userChoice, new DialogInterface.OnMultiChoiceClickListener() {



                            @Override
                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                // 捕捉用户的选择
                                text = "";
                                for (int i = 0; i < userChoice.length; i++) {
                                    if (userChoice[i]) { // 表示用户选择了
                                        text = text + hobbies[i];
                                    } else {// 用户没选择
                                        text = text.replace(hobbies[i], "");
                                    }
                                }
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 展示用户的选择
                                tv_res.setText(text);
                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .create()
                        .show();
            }
        });
    }
}

ImageView

图片嵌入框

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/358043.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数据结构与算法(Java版) | 线性结构和非线性结构

之前&#xff0c;我们说过&#xff0c;数据结构是算法的基础&#xff0c;因此接下来在这一讲我就要来给大家重点介绍一下数据结构了。 首先&#xff0c;大家需要知道的是&#xff0c;数据结构包括两部分&#xff0c;即线性结构和非线性结构。知道这点之后&#xff0c;接下来我…

flex一把梭

flex 使用display:flex&#xff0c;可以让一个元素变成弹性容器&#xff08;flex容器&#xff09;&#xff0c;该元素中的直接子元素成为弹性项&#xff08;flex项&#xff09; flex-direction 使用flex-direction可以控制flex容器的主轴的方向&#xff1a;垂直&#xff08;…

躬身入局,干货分享,2023年春招后端技术岗(Python)面试实战教程,Offer今始为君发

早春二月&#xff0c;研发倍忙&#xff0c;杂花生树&#xff0c;群鸥竟飞。为什么&#xff1f;因为春季招聘&#xff0c;无论是应届生&#xff0c;还是职场老鸟&#xff0c;都在摩拳擦掌&#xff0c;秣马厉兵&#xff0c;准备在面试场上一较身手&#xff0c;既分高下&#xff0…

Allegro如何让测量时显示双单位操作指导

Allegro如何让测量时显示双单位操作指导 在用Allegro做PCB设计的时候,时常会需要使用到测量命令,通常显示的一个单位,比如mil,如下图 当希望除了看到mil单位的值,又同时能够看到mm单位的值,省去换算的时间 具体设置如下 点击Setup点击User Preference

Linux服务:Nginx服务部署及基础配置

目录 一、Nginx介绍 1、Nginx简介 2、I/O模型相关概念 3、Nginx事件驱动模型 二、部署Nginx 1、yum部署Nginx 2、编译安装Nginx 三、Nginx使用 1、基础使用 2、nginx信号 四、web服务选择及优化 1、Nginx与Apache对比 2、Nginx服务调优 一、Nginx介绍 1、Nginx简…

SAP ABAP

方法一&#xff1a; REPORT ZDCH_09_TEST2. ************************************************************************ * DATEN DEFINITION * *********************************************************************…

【数据结构】二叉树-堆实现及其堆的应用(堆排序topK问题)

文章目录一、堆的概念及结构二、堆的实现1.结构的定义2.堆的初始化3.堆的插入4.堆的向上调整5.堆的删除6.堆的向下调整7.取出堆顶元素8.返回堆的元素个数9.判断堆是否为空10.打印堆中的数据11.堆的销毁三、完整代码1.Heap.h2.Heap.c3.test.c四、堆排序1.堆排序2.建堆3.选数4.完…

Shopee、ebay、亚马逊等跨境卖家了解测评的一篇干货

随着时代的发展&#xff0c;大家越来越喜欢网购&#xff0c;国外也有亚马逊、沃尔码、阿里国际、速卖通、ebay、shopee、Lazada、ozon、temu等等&#xff0c;而国外这些平台也有很大的市场&#xff0c;跨境电商也随时诞生&#xff0c;而当今社会环境实体生意越来越难做&#xf…

DAMA认证|数据治理产业上规模需要做到“三化”

数据治理是开启数据安全体系化建设的第一步&#xff0c;需要从产业层面做大做强&#xff0c;支撑数据安全整体框架&#xff0c;为数据流通提供安全保障&#xff0c;推动促进数字化产业进一步发展。 规模化发展是数据治理产业的瓶颈&#xff0c;行业数字化业务的复杂性和过多的定…

k8s安装tekton,编写task

文章目录一、官方安装二、国内资源安装安装tekton安装dashboard安装CLI三、demo编写task.yaml编写taskRun.yaml使用tkn命令查看参考文章一、官方安装 地址&#xff1a;https://tekton.dev/docs/installation/pipelines/#installing-tekton-pipelines-on-kubernetes 注意&#…

Spring MVC 源码之MultipartResolver 组件

MultipartResolver 组件&#xff0c;内容类型( Content-Type )为 multipart/* 的请求的解析器&#xff0c;主要解析文件上传的请求。例如&#xff0c;MultipartResolver 会将 HttpServletRequest 封装成 MultipartHttpServletRequest 对象&#xff0c;便于获取参数信息以及上传…

【NVMEM子系统】二、NVMEM驱动框架

个人主页&#xff1a;董哥聊技术我是董哥&#xff0c;嵌入式领域新星创作者创作理念&#xff1a;专注分享高质量嵌入式文章&#xff0c;让大家读有所得&#xff01;文章目录1、前言2、驱动框架3、源码目录结构4、用户空间下的目录结构1、前言 NVMEM SUBSYSTEM&#xff0c;该子系…

视频片段怎么做成gif图?快试试这2种方法

动态gif图片作为当下非常常用的表情包&#xff0c;其丰富的内容生动的画面深受大众喜爱。那么&#xff0c;当我们想要将电影或是电视剧中的某一片段做成gif动态图片的时候&#xff0c;要如何操作呢&#xff1f;接下来&#xff0c;给大家分享两招视频转化gif的小窍门–使用【GIF…

【力扣-Python-1】两数之和(easy)

https://leetcode.cn/problems/two-sum/题目描述给定一个整数数组 nums 和一个整数目标值 target&#xff0c;请你在该数组中找出和为目标值 target 的那两个整数&#xff0c;并返回它们的数组下标。你可以假设每种输入只会对应一个答案。但是&#xff0c;数组中同一个元素在答…

uboot下实现U盘自动升级程序的思路分析(基于USB系统、eMMC系统、FAT32文件系统)

1、常见的升级方式 1.1、应用程序升级 优点&#xff1a;在图形化界面操作&#xff0c;只需要选中升级文件并点击升级即可&#xff0c;操作简单&#xff1b; 缺点&#xff1a;应用程序必须能正常启动&#xff0c;当程序出现bug就不能升级&#xff0c;可靠性差&#xff1b; 总结…

旺店通与金蝶云星空对接集成采购入库单接口

旺店通旗舰奇门与金蝶云星空对接集成采购入库单查询连通销售退货新增V1(12-采购入库单集成方案-P)数据源系统:旺店通旗舰奇门旺店通是北京掌上先机网络科技有限公司旗下品牌&#xff0c;国内的零售云服务提供商&#xff0c;基于云计算SaaS服务模式&#xff0c;以体系化解决方案…

Prometheus集群分布式架构浅析

集群行为是一种常见于自然界中鱼群、鸟群、蜂群等低等群居生物的集体行为&#xff0c;受此启发形成了无人机集群的概念。无人机集群不是多无人机间的简单编队&#xff0c;而是通过必要的控制策略使之产生集群协同效应&#xff0c;从而具备执行复杂多变、危险任务的能力。目前无…

【C++】AVLTree——高度平衡二叉搜索树

文章目录一、AVL树的概念二、AVL树节点的定义三、AVL树的插入四、AVL树的旋转1.左单旋2.右单旋3.左右双旋4.右左双旋五、进行验证六、AVLTree的性能个人简介&#x1f4dd; &#x1f3c6;2022年度博客之星Top18;&#x1f3c6;2022社区之星Top2;的&#x1f947;C/C领域优质创作者…

JVM类加载子系统

1、类加载子系统在内存结构中所处的位置通过内存结构图&#xff0c;我们先知道类加载子系统所处的位置&#xff0c;做到心中有图。2、类加载器作用类加载器子系统负责从文件系统或者网络中加载Class文件&#xff0c;class文件在文件开头有特定的文件标识。ClassLoader只负责cla…

anaconda创建环境为空、修改默认环境位置

无论是用navigator还是命令行创建环境都无法指定python版本conda create -n test python3.9其实就是没有路径&#xff0c;添加几个镜像就好&#xff1a;conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels ht…