简体   繁体   English

定期自动在两个按钮之间切换焦点

[英]Auto change focus between two buttons periodically

package com.example.helloandroid;

import java.util.Timer;  
import java.util.TimerTask;  

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.util.Log;

import android.view.View;

import android.widget.Button;


public class HelloandroidActivity extends Activity {

/** Called when the activity is first created. */

Button mybtn0,mybtn1;

private Handler mhandler= new Handler();

Timer timer= new Timer();

String s=new String(" ");

int delay=0;

int period=5000;

int var=0;

@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Object o=null;
    // o.toString();
    // TextView tv = new TextView(this);
    // tv.setText("Hello Andriod");

    setContentView(R.layout.main);
    mybtn0= (Button)findViewById(R.id.number_button);
    //mybtn.requestFocus();
    mybtn0.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            mybtn0.clearFocus();
        }
    });
    // mybtn2= (Button)findViewById(R.id.contact_button);
    //setUpFocus();

    try
    {

    mybtn1=(Button)findViewById(R.id.contact_button);   
    //mybtn2= (Button)mybtn.focusSearch(View.FOCUS_DOWN);
    mybtn1.requestFocus();
    mybtn0.clearFocus();

    }
    catch(Exception e)
    {
            Log.e("focus change","focus failed",e);
    } 


    timer.scheduleAtFixedRate(new TimerTask(){

        public void run()
        { 

            switch(var)
            {

            case 0: mybtn0.clearFocus();
                    mybtn1.requestFocus();
                    break;

            case 1: mybtn1.clearFocus();
                    mybtn0.requestFocus();
                break;
            default: break;
            }

            var= (var+1)%2;


        }

    }, delay, period);

}

I have used the above code to accomplish auto change focus between two buttons using timer. 我已经使用上面的代码使用计时器来完成两个按钮之间的自动更改焦点。 but I am getting "called from wrong thread Exception". 但是我收到“从错误的线程异常调用”。 I think it is because of timer. 我认为是因为计时器。 please someone correct this code and as well provide the sample code on how to implement this with Handler class. 请有人更正此代码,并提供有关如何通过Handler类实现此示例代码。

Change your timer .schedule() to look like this 更改计时器.schedule()看起来像这样

    timer.scheduleAtFixedRate(new TimerTask(){

    public void run()
    { 


        mHandler.sendEmptyMessage(var); // <--- I suggest you camelCase your variable names so "mhandler" should be "mHandler", it will make them easier to read at a glance.
        var= (var+1)%2;


    }

}, delay, period);

Then add this handler instantiation to your onCreate() method some where: 然后将此处理程序实例添加到您的onCreate()方法中,其中:

mHandler = new Handler() {
     public void handleMessage(Message msg) {
            if(msg.what == 0){
                mybtn0.clearFocus();
                mybtn1.requestFocus();
            }else if(msg.what == 1){
                mybtn1.clearFocus();
                mybtn0.requestFocus();
            }
     }
}

You could also skip the TimerTask all together and just do the scheduling with a Handler along. 您也可以一起跳过TimerTask,并随同Handler一起进行调度。 Look at the .sendEmptyMessageDelayed() method 查看.sendEmptyMessageDelayed()方法

That code needs to execute on UI thread. 该代码需要在UI线程上执行。 There is Activity.runOnUiThread Activity.runOnUiThread

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM