欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

列举安卓常用View视图

程序员文章站 2022-04-11 18:50:59
本篇常用的View如图中所示:xml布局代码

本篇常用的View如图中所示:

列举安卓常用View视图列举安卓常用View视图列举安卓常用View视图列举安卓常用View视图列举安卓常用View视图

列举安卓常用View视图列举安卓常用View视图

xml布局代码

<?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:text="@string/yiban_dialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showYiban"/>

    <Button
        android:text="显示单选列表AlertDialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showLiebiao"/>

    <Button
        android:text="显示自定义AlertDialog"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showDingYi"/>

    <Button
        android:layout_marginTop="20dp"
        android:text="显示圆形进度ProgressDialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showYuanXing"/>

    <Button
        android:text="显示水平进度ProgressDialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showShuiPing"/>

    <Button
        android:layout_marginTop="20dp"
        android:text="显示DatePickerDialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showDate"/>

    <Button
        android:text="显示TimerPickerDialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showTime"/>

</LinearLayout>

自定义dialog-xml

<?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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:src="@drawable/title" />

    <EditText
        android:id="@+id/et_dialog_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名" />

    <EditText
        android:id="@+id/et_dialog_pwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:inputType="textPassword" />

</LinearLayout>

java代码

public class DialogActivity extends AppCompatActivity {

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

    public void showYiban(View v) {
        new AlertDialog.Builder(this)
                .setTitle("删除数据")
                .setPositiveButton("取消", (onClickDialogInterface, which) -> {
                    Toast.makeText(DialogActivity.this, "取消删除", Toast.LENGTH_SHORT).show();
                })
                .setNegativeButton("删除", (onClickDialogInterface, which) -> {
                    Toast.makeText(DialogActivity.this, "删除数据", Toast.LENGTH_SHORT).show();
                })
                .show();
    }

    private String choice;

    public void showLiebiao(View v) {
        final String[] color = {"红色", "绿色", "蓝色", "橙色"};

        new AlertDialog.Builder(this)
                .setTitle("选择背景颜色")
                .setPositiveButton("取消", (dialog, which) -> {
                    Toast.makeText(DialogActivity.this, "您取消选择了" + choice, Toast.LENGTH_SHORT).show();
                })
                .setNegativeButton("确定", (dialog, which) -> {
                    Toast.makeText(DialogActivity.this, "您选择了" + choice, Toast.LENGTH_SHORT).show();
                })
                .setSingleChoiceItems(color, 0, (dialog, which) -> {
//                    dialog.dismiss();
                    choice = color[which];
                })
                .show();
    }

    public void showDingYi(View v) {
        View view = View.inflate(this, R.layout.view_dialog, null);
        final EditText et_dialog_username = view.findViewById(R.id.et_dialog_username);
        final EditText et_dialog_pwd = view.findViewById(R.id.et_dialog_pwd);
        new AlertDialog.Builder(this)
                .setView(view)
                .setPositiveButton("取消", (dialog, which) -> {
                })
                .setNegativeButton("确定", (dialog, which) -> {
                    final String username = et_dialog_username.getText().toString();
                    final String pwd = et_dialog_pwd.getText().toString();
                    Toast.makeText(DialogActivity.this, username + ":" + pwd, Toast.LENGTH_SHORT).show();
                })
                .show();
    }

    public void showYuanXing(View view) {
        final ProgressDialog dialog = ProgressDialog.show(this, "数据下载", "数据下载中...");
        new Thread(() -> {
            for (int i = 0; i < 20; i++) {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            dialog.dismiss();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(DialogActivity.this, "加载完成", Toast.LENGTH_SHORT).show();
                }
            });

        }).start();
    }

    public void showShuiPing(View view) {
        ProgressDialog pd = new ProgressDialog(this);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                int time = 100;
                for (int i = 0; i < time; i++) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    pd.setProgress(pd.getProgress() + 1);
                }
                pd.dismiss();
            }
        }).start();
    }

    Calendar c = Calendar.getInstance();

    public void showDate(View view) {

        new DatePickerDialog(this, (datePicker, year, monthOfYear, dayOfMonth) -> {
            Toast.makeText(DialogActivity.this, year + "-" + (monthOfYear + 1) + "-" + dayOfMonth, Toast.LENGTH_SHORT).show();
        }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show();
    }

    public void showTime(View view) {

        new TimePickerDialog(this, (timePicker, hourOfDay, minute) -> {
            Toast.makeText(DialogActivity.this, hourOfDay + ":" + minute, Toast.LENGTH_SHORT).show();
        }, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true).show();
    }

} 

本文地址:https://blog.csdn.net/we1less/article/details/107734671