Android基础-(整体布局添加进度条和Toast)

1.添加Toast 如果当前名字和密码为空就报Toast

//1.判断姓名, 密码是否为空
        EditText nameEdt = findViewById(R.id.name);
        EditText pwdEdt = findViewById(R.id.pwd);
        ProgressBar proBar = findViewById(R.id.pro_bar);

        String name = nameEdt.getText().toString();
        String pwd = pwdEdt.getText().toString();
        if (name.equals("") || pwd.equals("")) {
            //2.如果为空,则提示
            //无焦点提示
            //参数1: 环境上下文
            Toast.makeText(this, "姓名或者密码不能为空", Toast.LENGTH_LONG).show(); 

2.否者就显示进度条,并且刷新进度条

else {
            //3.都不为空, 则出现进度条
          proBar.setVisibility(View.VISIBLE);
          new Thread() {
              @Override
              public void run() {
                  for (int i = 0; i < 100; i++) {
                      proBar.setProgress(i);
                      try {
                          Thread.sleep(30);
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }

                  }
              }
          }.start();

整体代码和xml布局

1.xml布局

<?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"
    android:background="@mipmap/bg"
    tools:context=".MainActivity"
    android:gravity="center_horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sign up"
        android:layout_marginTop="70dp"
        android:visibility="invisible"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Imooc Imooc Imooc Imooc\n Imooc Imooc"
        android:layout_margin="20dp"
        android:gravity="center_horizontal"/>

    <!--
    android:src="" 指定前景图片资源
    android:backgroud="" 设置背景
    -->
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/add_photo"
        android:layout_margin="10dp"/>

    <ProgressBar
        android:id="@+id/pro_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:visibility="invisible"
        android:layout_margin="10dp"/>

    <!--
    android:inputType 输入类型
        textPassowrd 密码
        number 只能有正整数
        numberSigned 只能输入整数
        numberDecimal 小数
    -->

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="68dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="25dp"
        android:hint="Name and Surname"
        android:gravity="center"
        android:textColorHint="#cccccc"
        android:background="@mipmap/border"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="68dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="25dp"
        android:hint="Emial Address"
        android:gravity="center"
        android:textColorHint="#cccccc"
        android:background="@mipmap/border"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="68dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="25dp"
        android:hint="Phone"
        android:gravity="center"
        android:textColorHint="#cccccc"
        android:inputType="phone"
        android:background="@mipmap/border"/>

    <EditText
        android:id="@+id/pwd"
        android:layout_width="match_parent"
        android:layout_height="68dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="25dp"
        android:hint="Password"
        android:gravity="center"
        android:textColorHint="#cccccc"
        android:inputType="textPassword"
        android:maxLength="12"
        android:background="@mipmap/border"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="68dp"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="30dp"
        android:background="@mipmap/btn"
        android:text="Register"
        android:onClick="register"
        app:backgroundTint="@color/teal_700" />



</LinearLayout>

整体代码

package com.example.uidemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

    public void register(View v) {
        //1.判断姓名, 密码是否为空
        EditText nameEdt = findViewById(R.id.name);
        EditText pwdEdt = findViewById(R.id.pwd);
        ProgressBar proBar = findViewById(R.id.pro_bar);

        String name = nameEdt.getText().toString();
        String pwd = pwdEdt.getText().toString();
        if (name.equals("") || pwd.equals("")) {
            //2.如果为空,则提示
            //无焦点提示
            //参数1: 环境上下文
            Toast.makeText(this, "姓名或者密码不能为空", Toast.LENGTH_LONG).show();
        } else {
            //3.都不为空, 则出现进度条
          proBar.setVisibility(View.VISIBLE);
          new Thread() {
              @Override
              public void run() {
                  for (int i = 0; i < 100; i++) {
                      proBar.setProgress(i);
                      try {
                          Thread.sleep(30);
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }

                  }
              }
          }.start();

        }



    }
}

Android基础-(整体布局添加进度条和Toast)

上一篇:Mybatis初步学习


下一篇:Mybatis搭建