您的位置:首页 > 其它

Exchange 2013sp1邮件系统部署-(二)

2014-09-01 11:08 447 查看
Timer跟TimerTask其实非常简单,用来执行一段规定时间内完成的任务,本文做的是设置Button的文本,首先activity创建的时候会显示默认文本“哈哈”,3S‘之后将Button文本改成“嘿嘿”,如果点击了Button,则定时器会进行新的一轮的定时任务,基于这个原理其实是可以开发类似于轮询的这种业务,但是使用这种方式不太好。
同时,点击Button会切换软键盘的状态,控制显示隐藏软键盘。

 

 

public class TestTimerTaskActivity extends Activity implements OnClickListener {
private static final int MESSAGE_WHAT = 100;
private Button btnClick;
private Timer mTimer;
private TimerTask mTimerTask;
private InputMethodManager inputMethodManager;

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

init();
mTimer = new Timer(true);
initTimerTask();

}

/**
* Initialize TimeTask
*/
private void initTimerTask() {
if (null != mTimer && null != mTimerTask)
mTimerTask.cancel();
mTimerTask = new MyTimerTask();
mTimer.schedule(mTimerTask, 3000);
}

/**
* Initialize the views
*/
private void init() {
btnClick = (Button) findViewById(R.id.btnClick);
btnClick.setOnClickListener(this);

inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
}

/**
* Handler Update UI
*/
Handler handler = new Handler() {

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == MESSAGE_WHAT) {
btnClick.setText(String.valueOf(msg.obj));
}
}

};

/**
* Override Button ClickListener
*/
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btnClick:
btnClick.setText("哈哈");
initTimerTask();
toggleSoftKeyBoard();
break;

default:
break;
}
}

/**
* toggleSoftKeyBoard status
*/
private void toggleSoftKeyBoard() {
if (inputMethodManager.isActive()) {
inputMethodManager.toggleSoftInput(
InputMethodManager.SHOW_IMPLICIT,
InputMethodManager.HIDE_NOT_ALWAYS);
}
}

/**
* Send message to UIThread
*
* @param what
*            message identifies
* @param str
*            Content of the message
*/
private void sendMssage(int what, String str) {
Message message = handler.obtainMessage(what, str);
handler.sendMessage(message);
}

/**
* Custom Derived class
*
* @author wallace
*
*/
private class MyTimerTask extends TimerTask {

@Override
public void run() {
sendMssage(MESSAGE_WHAT, "嘿嘿");
}
}

}

 

 

如果你想在activity刚创建的时候就调出软键盘,上述切换软键盘状态的函数是不起作用的,要用这个函数,其实没有必要用这段代码显示软键盘,直接在manifest文件里配置activity的时候给activity配置android:windowSoftInputMode="stateVisible|adjustPan"这个属性,就能自动弹出软键盘

 

private void toggleSoftKeyBoard() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {

@Override
public void run() {
inputMethodManager.toggleSoftInput(
InputMethodManager.SHOW_IMPLICIT,
InputMethodManager.HIDE_NOT_ALWAYS);
}
}, 100);
}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: