为什么Viewflipper的速度不会改变?

时间:2016-10-10 05:48:32

标签: android

你好我试图从搜索栏传递变量来将viewflipper的种子设置为另一种活动,但我无法让速度改变。我是否通过了搜索栏值?是否有特殊方法从搜索栏调用变量?有没有使用搜索栏设置意图的特殊方法?

public class SlideShow extends AppCompatActivity {

ViewFlipper viewFlipper;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_slide_show);

    Intent intent = getIntent();
    int progress = intent.getExtras().getInt("progress");
    viewFlipper = (ViewFlipper)this.findViewById(R.id.viewFlipper);
    viewFlipper.setAutoStart(true);
    viewFlipper.setFlipInterval(progress);
    viewFlipper.startFlipping();
} }

public class MainActivity extends AppCompatActivity  {

SeekBar seekBar;
int progress;
Button button;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button)findViewById(R.id.button);
    seekBar = (SeekBar) findViewById(R.id.seekBar);
    seekBar.setMax(10);

    final Intent intent = new Intent(this, SlideShow.class);
    intent.putExtra("progress", progress);

    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            progress = seekBar.getProgress();
        }
    });

  button.setOnClickListener(new View.OnClickListener(){
      public void onClick(View view){
          if (view.getId() == R.id.button){
              startActivity(intent);
          }
  }
});}

1 个答案:

答案 0 :(得分:0)

您没有更新意图中的progress额外内容。更新progress变量还不够,您需要再次更新额外内容。

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
    progress = seekBar.getProgress();
    intent.putExtra("progress", progress);
}

这将是最快的解决方案。

无论如何,我说你在onClick监听器中创建整个意图(并从onCreate的主体中删除它)会更好:

button.setOnClickListener(new View.OnClickListener(){
    public void onClick(View view){
        if (view.getId() == R.id.button){
            final Intent intent = new Intent(MainActivity.this, SlideShow.class);
            intent.putExtra("progress", progress);
            startActivity(intent);
        }
    }
});