- 我有一个简单设计并放置在 MainActivity 中的进度条。
- 我有第二个活动来设置 ProgressBar 的最大值
我的问题是;如何使用public void onActivityResult(int requestCode, int resultCode, Intent data)
ProgressBar 的 setMax() 方法的返回值?
package com.example.example;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends Activity {
final int request_Code = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*I want to use value cames from onActivityResult() for setMax()
instead of default entered value -> 100 */
ProgressBar calorieBar = (ProgressBar) findViewById(R.id.progress1);
calorieBar.setMax(100);
calorieBar.setProgress(70);
//Go to another Activity and start
Button newPlan = (Button) findViewById(R.id.newPlanBTN);
newPlan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent("com.hakkikonu.dailycalorie.NEWPLAN");//new_plan activity
startActivityForResult(i, 1);
onResume();
}
});
}//end of onCreate
//Sets Calorie value Comes from NewPlan.java and makes it Toast to show user
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == request_Code) {
if (resultCode == RESULT_OK) {
Toast.makeText(this,"Adjusted to "+data.getData().toString(),
Toast.LENGTH_LONG).show();
// Do somethings here: get data and convert it to integer
}
}
}
}
新计划.Java
package com.hakkikonu.dailycalorie;
import com.hakkikonu.dailycalorie.R.layout;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
public class NewPlan extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_plan);
final TextView seekBarValue = (TextView)findViewById(R.id.seekBarValue);
//Plan Button Click Listener
final Button setPlanButton = (Button)findViewById(R.id.setPlanBTN);
setPlanButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
setPlanButton.setBackgroundColor(Color.YELLOW);
Intent calorieVal = new Intent();
//---set the data to pass back---
calorieVal.setData(Uri.parse(
seekBarValue.getText().toString()));
setResult(RESULT_OK, calorieVal);
//---closes the activity---
finish();
}
});//end SetPlan Button Listener
SeekBar seekBar = (SeekBar)findViewById(R.id.seekBar);
seekBar.setProgress(0); //initial value of seekBar
seekBar.incrementProgressBy(50); //increment step
seekBar.setMax(5000); //maximum possible calory for a day.
//seekBar Listener
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//text view @ top of the seekBar
progress = progress / 50;
progress = progress * 50;
if(progress == 0){
seekBarValue.setText(String.valueOf(progress)+" Calorie");
}
else{
seekBarValue.setText(String.valueOf(progress)+" Calories");
}
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
//Don't delete
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// Don't delete
}
});
}
}