I am trying to create a button that when clicked it will make the progress bar increase.
I have set the bar to a maximum of 100 and every time the button is selected, I'm hoping it will go up by 1/10.
Anyone able to help with this? My progress bar is just called 'progressBar'.
<ProgressBar
android:progressDrawable="@android:drawable/progress_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_above="@+id/imageView36"
android:max="100"
android:progress="10"
android:layout_alignEnd="@+id/btnGenerate"
android:layout_alignStart="@+id/btnGenerate" />
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_squiz);
/*Button button =
(Button) findViewById(R.id.btnGenerate);
MyOnClickListener Listener =
new MyOnClickListener();
button.setOnClickListener((View.OnClickListener) Listener);*/
//btnGenerate = (Button) findViewById(R.id.btnGenerate);
//btnGenerate.setOnClickListener(btnGenerateListener);
TextView ABshow = (TextView) findViewById(R.id.ABNumber);
TextView BCshow = (TextView) findViewById(R.id.BCNumber);
TextView Ashow = (TextView) findViewById(R.id.ANumber);
// btnRand.setOnClickListener(btnRandListener);
Random rnd = new Random();
int ABrandomNumber = rnd.nextInt(34) + 5; //random number between 5 and 39
//Toast.makeText(SineQuiz.this, "Rand num: " + String.valueOf(ABrandomNumber), Toast.LENGTH_SHORT).show();
ABshow.setText("" + String.valueOf(ABrandomNumber));
int BCrandomNumber = rnd.nextInt(20) + 5; //random number between 5 and 25
//Toast.makeText(SineQuiz.this, "Rand num: " + String.valueOf(BCrandomNumber), Toast.LENGTH_SHORT).show();
BCshow.setText("" + String.valueOf(BCrandomNumber));
int ArandomNumber = rnd.nextInt(25) + 5; //random number between 5 and 30
//Toast.makeText(SineQuiz.this, "Rand num: " + String.valueOf(ArandomNumber), Toast.LENGTH_SHORT).show();
Ashow.setText("" + String.valueOf(ArandomNumber));
/* button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
progressBar.setProgress(progressBar.getProgress() + 10);
}
});*/
}
public void setBtnGenerateListener (View view) {
progressBar.setProgress(progressBar.getProgress() + 10);}
/*public View.OnClickListener btnGenerateListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
progressBar.setProgress(progressBar.getProgress() + 10);
}
};*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_sine_quiz, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Sorry I don't know if the code is much help! And ideally, if this button could also generate the random numbers I have above that would be a bonus!
just set the progress to your progressBar:
progressBar.setProgress(progressBar.getProgress()+10);
To put it into your click event:
myButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
progressBar.setProgress(progressBar.getProgress()+10);
}
});
将此添加到按钮的单击侦听器中。
progressBar.setProgress(progressBar.getProgress()+10);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.