我正在掷骰子并使用随机函数我正在计算数字。 1s,2s,3s,4s,5s,6s打印出来。如果它需要太多时间,那么它将显示进度条。为什么我的android代码没有显示进度条虽然我在我的java代码中使用了进度条? Plz弄清楚错误。
Java代码
package dead.asynctaskprogressbar;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
EditText editText;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.textView);
editText=(EditText)findViewById(R.id.editText);
}
public class Asynctask extends AsyncTask<Integer,Integer,String>{
ProgressBar bar;
@Override
protected void onPreExecute() {
super.onPreExecute();
bar=new ProgressBar(MainActivity.this,null,android.R.attr.progressBarStyleHorizontal);
bar.setVisibility(View.VISIBLE);
bar.setMax(Integer.parseInt(editText.getText().toString().trim()));
}
@Override
protected String doInBackground(Integer... integers) {
Random random=new Random();
int ones=0,twos=0,threes=0,fours=0,fives=0,sixes=0,a;
double currentProgress=0;
double previousprogress=0;
for (int i=0;i<integers[0];i++){
currentProgress=(double)i/integers[0];
if (currentProgress-previousprogress>=0.02){
previousprogress=currentProgress;
publishProgress(i);
}
a= random.nextInt(6)+1;
switch (a){
case 1:
ones++;
break;
case 2:
twos++;
break;
case 3:
threes++;
break;
case 4:
fours++;
break;
case 5:
fives++;
break;
default:
sixes++;
}
}
String results="Ones :"+ones +"\n"+"Twos :"+twos +"\n"+"Threes :"+threes +"\n"+"Fours :"+fours +"\n"+"Fives :"+fives +"\n"+"Sixes :"+sixes +"\n";
return results;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
bar.setProgress(values[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
textView.setText(s);
}
}
public void Dice(View view){
int not=Integer.parseInt(editText.getText().toString().trim());
new Asynctask().execute(not);
}
}
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"
tools:context="dead.asynctaskprogressbar.MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center_horizontal"
android:hint="Dice"
android:inputType="textPersonName"
android:padding="10dp" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Dice"
android:text="Roll Dice" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="20dp" />
</LinearLayout>
答案 0 :(得分:0)
为什么我的Android代码没有显示进度条
您已动态创建 ProgressBar
,但您未在布局中添加 ProgressBar
,这就是为什么不显示
您需要在布局中添加 ProgressBar
尝试以下代码,它将正常工作
示例代码
public class MainActivity extends AppCompatActivity {
ProgressBar bar;
LinearLayout rootView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar=new ProgressBar(MainActivity.this,null,android.R.attr.progressBarStyleHorizontal);
rootView=findViewById(R.id.rootView);
rootView.addView(bar);
bar.setVisibility(View.GONE);
}
public class Asynctask extends AsyncTask<Integer,Integer,String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
bar.setVisibility(View.VISIBLE);
bar.setMax(Integer.parseInt(editText.getText().toString().trim()));
}
@Override
protected String doInBackground(Integer... integers) {
Random random=new Random();
int ones=0,twos=0,threes=0,fours=0,fives=0,sixes=0,a;
double currentProgress=0;
double previousprogress=0;
for (int i=0;i<integers[0];i++){
currentProgress=(double)i/integers[0];
if (currentProgress-previousprogress>=0.02){
previousprogress=currentProgress;
publishProgress(i);
}
a= random.nextInt(6)+1;
switch (a){
case 1:
ones++;
break;
case 2:
twos++;
break;
case 3:
threes++;
break;
case 4:
fours++;
break;
case 5:
fives++;
break;
default:
sixes++;
}
}
String results="Ones :"+ones +"\n"+"Twos :"+twos +"\n"+"Threes :"+threes +"\n"+"Fours :"+fours +"\n"+"Fives :"+fives +"\n"+"Sixes :"+sixes +"\n";
return results;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
bar.setProgress(values[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
textView.setText(s);
}
}
public void Dice(View view){
int not=Integer.parseInt(editText.getText().toString().trim());
new Asynctask().execute(not);
}
}