Android 如何在水平进度条中设置进度
我想让水平进度条从0移动到100。我在布局中添加了此代码。要移动此进度条,我应该做什么Android 如何在水平进度条中设置进度,android,progress-bar,android-progressbar,Android,Progress Bar,Android Progressbar,我想让水平进度条从0移动到100。我在布局中添加了此代码。要移动此进度条,我应该做什么 <ProgressBar android:id="@+id/progressBar" android:indeterminate="false" android:layout_width="300dp" android:layout_height="50dp" android:layout_marginTop="100dp
<ProgressBar
android:id="@+id/progressBar"
android:indeterminate="false"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginTop="100dp"
android:layout_marginLeft="50dp"
android:progress="0"
android:max="100"
style="@android:style/Widget.ProgressBar.Horizontal"
/>
您必须添加该功能
样品
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="116dp"
android:text="Do some stuff" />
<ProgressBar
style="@android:style/Widget.ProgressBar.Horizontal"
android:id="@+id/progressBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:progress="0"/>
</LinearLayout>
类MainActivity:AppCompatActivity(){
private var progressBarStatus=0
虚拟变量:Int=0
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//从布局文件中获取引用
var btnStartProgress=this.button1
var progressBar=this.progressBar1
//单击按钮后,启动任务
btnStartProgress.setOnClickListener{v->
//任务在线程上运行
线程(可运行{
//模拟某个操作的虚拟线程,该操作的进度可以跟踪
而(状态<100){
//执行一些虚拟操作
试一试{
假人=假人+25
线程。睡眠(1000)
}捕获(e:中断异常){
e、 printStackTrace()
}
//跟踪进度
progressBarStatus=虚拟
//更新进度条
progressBar.progress=progressBarStatus
}
}).start()
}
}
}请尝试以下代码:
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity {
Button btn;
private ProgressBar progressBar;
TextView txt;
Integer count =1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(100);
btn = (Button) findViewById(R.id.btn);
btn.setText("Start");
txt = (TextView) findViewById(R.id.output);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
count =1;
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
new MyTask().execute(100);
});
}
class MyTask extends AsyncTask<Integer, Integer, String> {
@Override
protected String doInBackground(Integer... params) {
for (count=1 ; count <= params[0]; count++) {
try {
Thread.sleep(1000);
publishProgress(count);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return "Task Completed.";
}
@Override
protected void onPostExecute(String result) {
progressBar.setVisibility(View.GONE);
txt.setText(result);
btn.setText("Restart");
}
@Override
protected void onPreExecute() {
txt.setText("Task Starting...");
}
@Override
protected void onProgressUpdate(Integer... values) {
txt.setText("Running..."+ values[0]);
progressBar.setProgress(values[0]);
}
}
}
导入android.app.Activity;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.ProgressBar;
导入android.widget.TextView;
公共类MainActivity扩展了活动{
按钮btn;
私人ProgressBar ProgressBar;
文本视图;
整数计数=1;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_活动);
progressBar=(progressBar)findViewById(R.id.progressBar);
设置最大进度条(100);
btn=(按钮)findViewById(R.id.btn);
btn.setText(“开始”);
txt=(TextView)findViewById(R.id.output);
btn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
计数=1;
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
new MyTask().execute(100);
});
}
类MyTask扩展了AsyncTask{
@凌驾
受保护字符串doInBackground(整数…参数){
对于(count=1;count From code)您需要在ProgressBar视图对象上调用setProgress(int)
,并且进度条将随之移动这是否回答了您的问题?不,这不是我问题的答案。在kotlin代码中按id查找进度视图,并在循环中将进度值从1设置为100ProgressBar.setProgress(value)
谢谢,你能帮我讲一下密码吗?你说的是谁?这是一门课
private var progressBarStatus = 0
var dummy:Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// get the references from layout file
var btnStartProgress = this.button1
var progressBar = this.progressBar1
// when button is clicked, start the task
btnStartProgress.setOnClickListener { v ->
// task is run on a thread
Thread(Runnable {
// dummy thread mimicking some operation whose progress can be tracked
while (progressBarStatus < 100) {
// performing some dummy operation
try {
dummy = dummy+25
Thread.sleep(1000)
} catch (e: InterruptedException) {
e.printStackTrace()
}
// tracking progress
progressBarStatus = dummy
// Updating the progress bar
progressBar.progress = progressBarStatus
}
}).start()
}
}
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity {
Button btn;
private ProgressBar progressBar;
TextView txt;
Integer count =1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(100);
btn = (Button) findViewById(R.id.btn);
btn.setText("Start");
txt = (TextView) findViewById(R.id.output);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
count =1;
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
new MyTask().execute(100);
});
}
class MyTask extends AsyncTask<Integer, Integer, String> {
@Override
protected String doInBackground(Integer... params) {
for (count=1 ; count <= params[0]; count++) {
try {
Thread.sleep(1000);
publishProgress(count);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return "Task Completed.";
}
@Override
protected void onPostExecute(String result) {
progressBar.setVisibility(View.GONE);
txt.setText(result);
btn.setText("Restart");
}
@Override
protected void onPreExecute() {
txt.setText("Task Starting...");
}
@Override
protected void onProgressUpdate(Integer... values) {
txt.setText("Running..."+ values[0]);
progressBar.setProgress(values[0]);
}
}
}