上一篇的ProgressBar練習是以Horizont的方式
這篇是以環形轉圈圈的ProgressBar練習
利用按下Button後跳出Progress Bar,類似現在許多介面換頁都會以ProgressBar顯示
首先先在Activity_main.xml新增一個Login的Button
範例如下:
<?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:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="100dp"
android:text="LOGIN"
android:textSize="50dp" />
</LinearLayout>
接著在res/layout下新增一個xml檔來設計按下Button後跳出Progress Bar的樣式
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="100sp"
android:layout_height="100sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.41" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Loading..."
android:textSize="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressBar" />
</androidx.constraintlayout.widget.ConstraintLayout>
設計完Progress Bar後先新增一個java檔來設定跳出的Progress Bar的時間
package com.example.itprogress2;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.LayoutInflater;
public class login {
private Activity activity;
private AlertDialog alertDialog;
login(Activity myactivity) {
activity = myactivity;
}
void startlogin(){
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
LayoutInflater inflater = activity.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.in,null));
//等於true時滑鼠點一下即可暫停
//等於false時則不會暫停
builder.setCancelable(true);
alertDialog = builder.create();
alertDialog.show();
}
void dismisslogin(){
alertDialog.dismiss();
}
}
接著回到MainActivity.java來設定Button按下時的設定
package com.example.itprogress2;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
login login = new login(MainActivity.this);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
login.startlogin();
}
});
}
}
這樣就好囉