首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >值在onClick中,而不是在onResume中修复

值在onClick中,而不是在onResume中修复
EN

Stack Overflow用户
提问于 2013-11-12 07:25:25
回答 1查看 587关注 0票数 0

我是android的新手。我写了一个程序,球通过加速度传感器移动,这是很好的工作。为此,我使用计时器和调度方法(调度(mTimerTask,延迟,周期);)我的屏幕上有两个按钮,当用户单击“正”按钮时,“时间段”和“延迟值”增加,当单击“负”按钮时,这些值减少。我编写了这段代码,但是onClick方法中的更改值并没有修复onResume方法中的值。我知道onResume方法是生命周期的一部分,但我不知道如何解决我的问题!请帮助我:)谢谢

代码语言:javascript
复制
public class MainActivity extends Activity {

    float result=0;
    BallView ball=null;
    Handler redrawHandler=new Handler();
    //
    Timer mTimer;
    TimerTask mTimerTask;
    //
    int mSrcWidth,mSrcHeight;
    PointF mBallPos,mBallSpd;
    //
    Button positiveBtn;
    Button negativeBtn;
    //
    int delay=10;
    int period=10;

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

        positiveBtn=(Button)findViewById(R.id.positiveBtn);
        negativeBtn=(Button)findViewById(R.id.negativeBtn);

        Log.e("onclick", "in oncreate");

        getWindow().setFlags(0xFFFFFFFF, LayoutParams.FLAG_FULLSCREEN|LayoutParams.FLAG_KEEP_SCREEN_ON);

        final FrameLayout frame=(FrameLayout)findViewById(R.id.main_view);

        //
        Display display=getWindowManager().getDefaultDisplay();

        mSrcWidth=display.getWidth();
        mSrcHeight=display.getHeight();
        mBallPos=new PointF();
        mBallSpd=new PointF();

        mBallPos.x=mSrcWidth/2;
        mBallPos.y=mSrcHeight/2;
        mBallSpd.x=0;
        mBallSpd.y=0;

        ball=new BallView(this,mBallPos.x,mBallPos.y,10);
        frame.addView(ball);
        //
        ball.invalidate();


        positiveBtn.setOnClickListener(onClickListener);
        negativeBtn.setOnClickListener(onClickListener);

        SensorManager sm=(SensorManager)getSystemService(Context.SENSOR_SERVICE);

        sm.registerListener(new SensorEventListener(){

            public void onAccuracyChanged(Sensor sensor, int accuracy) {
                // TODO Auto-generated method stub

            }

            public void onSensorChanged(SensorEvent event) {
                // TODO Auto-generated method stub
                //
                mBallSpd.x=-event.values[0];
                mBallSpd.y=event.values[1];
            }
        //  
        },sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0), SensorManager.SENSOR_DELAY_NORMAL);

        Log.e("onclick", "in onsensor");
    }

    private OnClickListener onClickListener=new OnClickListener(){

        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch(v.getId()){
            case R.id.negativeBtn:
                if(delay<=5&&period<=5)
                    delay=period=5;
                else{
                    delay=delay-10;
                    period=period-10;
                }
            break;
            case R.id.positiveBtn:
                if(delay>=50&&period>=50)
                    delay=period=150;
                else{
                    delay=delay+10;
                    period=period+10;
                }
                break;
            }

            Log.e("delay","="+ delay);
            Log.e("period","="+ period);
        }


    } ;

    @Override
    public void onPause(){
        mTimer.cancel();
        mTimer=null;
        mTimerTask=null;
        super.onPause();
    }

    @Override
    public void onResume(){

        mTimer=new Timer();
        mTimerTask=new TimerTask(){
            public void run(){

                //Log.e("in ", "onResume()");

                //positiveBtn.setOnClickListener(onClickListener);
                //negativeBtn.setOnClickListener(onClickListener);

                mBallPos.x+=mBallSpd.x;
                mBallPos.y+=mBallSpd.y;
                if(mBallPos.x>=mSrcWidth)
                    mBallPos.x=mSrcWidth;
                if(mBallPos.y>=mSrcHeight)
                    mBallPos.y=mSrcHeight;
                if(mBallPos.x<=0)
                    mBallPos.x=0;
                if(mBallPos.y<=0)
                    mBallPos.y=0;

                ball.x=mBallPos.x;
                ball.y=mBallPos.y;

                redrawHandler.post(new Runnable(){

                    public void run() {
                        // TODO Auto-generated method stub
                        ball.invalidate();
                    }

                });
            }
        };

        mTimer.schedule(mTimerTask, delay,period);
        super.onResume();
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-12 08:51:12

像这样实现onSaveInstanceState

代码语言:javascript
复制
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putInt("STATE_DELAY", delay);
    savedInstanceState.putInt("STATE_PERIOD", period);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

然后在您的onCreate中尝试恢复它们(如果它们已经设置),如下所示:

代码语言:javascript
复制
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
    // Restore value of members from saved state
    delay = savedInstanceState.getInt("STATE_DELAY");
    period = savedInstanceState.getInt("STATE_PERIOD");
}

最后,正如我在问题的评论中所写的,请看一下重新创造一个活动培训页面。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19923260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档