2
  1. 我有一个简单设计并放置在 MainActivity 中的进度条。
  2. 我有第二个活动来设置 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

        }

    });

}

}
4

2 回答 2

2

假设您的第二个Activity 将 aint作为其额外内容,并"YOUR_KEY"在其 result 中添加一个键Intent,只需将进度条的最大值设置为该int额外内容:

//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();

               ((ProgressBar) findViewById(R.id.progress1)).setMax (data.getIntExtra ("YOUR_KEY", 0));


            }            
        }
    }

为了简单起见(我想说):

public class NewPlan extends Activity{
private int myProgress = 0;//add this line

然后在更改的侦听器中:

 progress = progress / 50;
 progress = progress * 50;
 myProgress = progress;//add this line

然后设置数据,更改为:

Intent calorieVal = new Intent();

calorieVal.putExtra ("YOUR_KEY", myProgress);
setResult(RESULT_OK, calorieVal);

//---closes the activity---
finish(); 
于 2013-02-25T01:10:27.287 回答
1

这是您在 NEWPLAN Activity 中设置结果意图的方式:

Intent intent = new Intent();
intent.putExtra("myInt", 12);
setResult(Activity.RESULT_OK, intent);

finish();

然后在 MainActivity 中,您使用以下方法获取该数据:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
     if (requestCode == yourRequestCode && RESULT_OK) {                
         int myInt = data.getIntExtra("myInt", 0);
         // do more if you wish
     }
 }
于 2013-02-25T01:14:43.940 回答