Android移动开发——进度条ProgressBar
——木梓婕
1、题目要求:
实现对进度条进度变化的控制
2、效果截图:
3、XML代码:
<?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" tools:context=".MainActivity"> <ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="250dp" android:layout_height="wrap_content" android:max="200" android:progress="50" android:layout_marginTop="32dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:text="增加" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/progressBar" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="减少" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button1" /> </androidx.constraintlayout.widget.ConstraintLayout>
4、Java代码:
package com.example.myapplication1_5; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; public class MainActivity extends AppCompatActivity{ ProgressBar progressBar; Button btn1,btn2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progressBar=findViewById(R.id.progressBar); btn1=findViewById(R.id.button1); btn2=findViewById(R.id.button2); btn1.setOnClickListener(new click1()); btn2.setOnClickListener(new click2()); } class click1 implements View.OnClickListener { public void onClick(View v) { progressBar.incrementProgressBy(5); } } class click2 implements View.OnClickListener { public void onClick(View v) { progressBar.incrementProgressBy(-5); } } }
5、补充——(使这2个按钮共用一个监听器):
Java代码:
package com.example.myapplication1_5; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ ProgressBar progressBar; Button btn1,btn2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progressBar=findViewById(R.id.progressBar); btn1=findViewById(R.id.button1); btn2=findViewById(R.id.button2); //btn1.setOnClickListener(new click1()); //btn2.setOnClickListener(new click2()); btn1.setOnClickListener(this); btn2.setOnClickListener(this); } /*class click1 implements View.OnClickListener { public void onClick(View v) { progressBar.incrementProgressBy(5); } } class click2 implements View.OnClickListener { public void onClick(View v) { progressBar.incrementProgressBy(-5); } }*/ public void onClick(View v) { switch (v.getId()) { case R.id.button1: progressBar.incrementProgressBy(5); break; case R.id.button2: progressBar.incrementProgressBy(-5); break; } } }