在文本完成后将进度条设置为增加

时间:2017-06-27 03:06:06

标签: java android android-layout

我在底部创建了一个带有进度条的表单。 我知道如何在单击按钮后指定要设置为某个值的进度条。 我的问题是,在填写商家名称,商家电子邮件等后,如何才能提高进度条。

到目前为止,这是我的代码 enter image description here

以下主要活动:

package com.example.formupload;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;

 <?xml version="1.0" encoding="utf-8"?>
 <android.support.constraint.ConstraintLayout
 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="com.example.formupload.MainActivity">  


   <EditText
    android:id="@+id/businame"
    android:layout_width="368dp"
    android:layout_height="45dp"
    android:ems="10"
    android:hint="Business Name"
    android:inputType="textPersonName"
    android:singleLine="true"
    android:textStyle="bold"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="16dp" />

    <EditText
    android:id="@+id/busiemail"
    android:layout_width="368dp"
    android:layout_height="45dp"
    android:layout_marginTop="15dp"
    android:ems="10"
    android:hint="Business Email"
    android:inputType="textEmailAddress"
    android:singleLine="true"
    android:textStyle="bold"
    app:layout_constraintTop_toBottomOf="@+id/businame"
    tools:layout_editor_absoluteX="8dp" />

    <EditText
    android:id="@+id/busiadd"
    android:layout_width="231dp"
    android:layout_height="48dp"
    android:layout_marginTop="8dp"
    android:ems="10"
    android:hint="Address"
    android:inputType="textPostalAddress"
    android:singleLine="true"
    android:textStyle="bold"
    app:layout_constraintTop_toBottomOf="@+id/editText6"
    tools:layout_editor_absoluteX="8dp" />

    <Button
    android:id="@+id/busiloc"
    style="@style/Widget.AppCompat.Button"
    android:layout_width="130dp"
    android:layout_height="49dp"
    android:backgroundTint="@android:color/holo_blue_bright"
    android:elevation="0dp"
    android:text="Get Location"
    android:textAlignment="center"
    android:visibility="visible"
    android:layout_marginTop="-27dp"
    app:layout_constraintTop_toBottomOf="@+id/editText6"
    app:layout_constraintLeft_toRightOf="@+id/busiadd"
    android:layout_marginLeft="-370dp" />

    <Button
    android:id="@+id/importgal"
    android:layout_width="230dp"
    android:layout_height="59dp"
    android:layout_marginTop="29dp"
    android:text="Import From Gallery"
    android:onClick="btnClick"
    app:layout_constraintTop_toBottomOf="@+id/busiadd"
    tools:layout_editor_absoluteX="8dp" />

   <EditText
    android:id="@+id/busitype"
    android:layout_width="371dp"
    android:layout_height="83dp"
    android:layout_marginTop="24dp"
    android:contentDescription="Indentify Business type"
    android:ems="10"
    android:hint="Briefly State Business Nature"
    android:inputType="textMultiLine"
    android:textAlignment="viewStart"
    app:layout_constraintTop_toBottomOf="@+id/importgal"
    tools:layout_editor_absoluteX="7dp" />

    <EditText
    android:id="@+id/editText6"
    android:layout_width="368dp"
    android:layout_height="45dp"
    android:layout_marginTop="8dp"
    android:autoText="true"
    android:ems="10"
    android:hint="Phone Number"
    android:inputType="number"
    android:singleLine="true"
    app:layout_constraintTop_toBottomOf="@+id/busiemail"
    tools:layout_editor_absoluteX="8dp" />

    <Button
    android:id="@+id/btnupload"
    android:layout_width="105dp"
    android:layout_height="48dp"
    android:layout_marginTop="10dp"
    android:text="Upload"
    app:layout_constraintTop_toBottomOf="@+id/busitype"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="@+id/busitype"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="@+id/busitype"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp" />

    <ProgressBar
    android:id="@+id/formprog"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="261dp"
    android:layout_height="37dp"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/btnupload"
    android:layout_marginLeft="-75dp"
    app:layout_constraintLeft_toLeftOf="@+id/btnupload"
    android:layout_marginRight="0dp"
    app:layout_constraintRight_toRightOf="@+id/btnupload"
    app:layout_constraintHorizontal_bias="0.487" />

<ImageView
    android:id="@+id/Imageupload"
    android:layout_width="128dp"
    android:layout_height="80dp"
    android:layout_marginLeft="2dp"
    android:layout_marginTop="15dp"
    app:layout_constraintLeft_toRightOf="@+id/importgal"
    app:layout_constraintTop_toBottomOf="@+id/busiloc"
     />

   </android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

Rahul Gupta是对的。只需使用

progressBar.setMax(nb_field_to_fill);

然后就这样做:

    private ProgressBar pgProgressBar;
    private boolean mEmailInputIsEmpty = true;

    private myMethod() {
        mEmailInput.addTextChangedListener(new TextWatcher() {
        @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    if (mEmailInputIsEmpty) {
                        pgProgressBar.setProgress(pgProgressBar.getProgress() + 1);
                        mEmailInputIsEmpty = false;
                    }
                } else {
                    pgProgressBar.setProgress(pgProgressBar.getProgress() -1);
                    mEmailInputIsEmpty = true;
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

在这种情况下,我们希望在填充mEmailInput时更新pgProgressBar。 boolean mEmailInputIsEmpty用于防止在EditText中每次输入时更新progressBar。