android 线程中的ui问题 Handler的基本使用 关于获取动态时间在ui的问题

话说学习android,开始也就学习学习传值,页面跳转,界面==,要学习还是需要研究的,为了做那个啥闹钟的系统,我试着在android的里面使用线程。恩,先贴之前的。

package com.love;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Map;import java.util.Set;import com.love.R;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.widget.EditText;public class Time extends Activity {private EditText EditText01;MyView myview;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);EditText01 = (EditText) findViewById(R.id.EditText01);thread1 aa = new thread1();new Thread(aa).start();}class thread1 implements Runnable {@Overridepublic void run() {String s1 = "2010-12-13 07:49:00";String s2;Date d2;myview.postInvalidate(); while (true) {d2 = new Date();s2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(d2);if (s2.equals(s1)) {EditText01.setText("shuaiqi");//这里修改了ui文本显示,就各种异常了。}try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}}}

 

跟之前netbeans一样,这让我很纠结。按道理是没问题的哇,可是就是这么的悲剧。

我查看了挺多资料的,发现恩,在其他线程中不能修改ui,只能在ui线程中修改,

什么是UI线程呢?

有用户界面的一般称为UI线程,没有界面的称之为工作者线程,当然这两者之间还是有区别的,UI线程因为有界面,所以系统会给它维护一个消息队列,工作者线程就没有消息队列.

因为有界面的关系,所以不能在线程中改,那这么办呢。。

恩,在android中是利用Handler来实现UI线程的更新的。 

自己看代码吧。。。

package com.love;

 

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

import java.util.Set;

import com.love.R;

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.EditText;

import android.widget.Spinner;

import android.widget.TextView;

 

public class Time extends Activity {

private EditText EditText01;

private EditText EditText02;

private TextView TextView01;

private Button Button01;

private Spinner Spinner_001;

private Spinner Spinner_002;

private Spinner Spinner_011;

private Spinner Spinner_012;

private EditText EditText_001;

private EditText EditText_002;

private CheckBox CheckBox01;

private boolean b1=false;

private String time;

private String s1 = "";

private String s2 = "";

private Date d1;

private Map<String, String> dd = new HashMap<String, String>();

private Handler mHandler = new Handler() {

public void handleMessage(Message msg) {

switch (msg.what) {

case 1:

updateTitle();

break;

}

};

};

 

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

EditText01 = (EditText) findViewById(R.id.EditText01);

EditText02 = (EditText) findViewById(R.id.EditText02);

EditText_001 = (EditText) findViewById(R.id.EditText_001);

EditText_002 = (EditText) findViewById(R.id.EditText_002);

TextView01 = (TextView) findViewById(R.id.TextView01);

Spinner_001=(Spinner)findViewById(R.id.Spinner_001);

Spinner_002=(Spinner)findViewById(R.id.Spinner_002);

Spinner_011=(Spinner)findViewById(R.id.Spinner_011);

Spinner_012=(Spinner)findViewById(R.id.Spinner_012);

CheckBox01=(CheckBox)findViewById(R.id.CheckBox01);

Button01=(Button)findViewById(R.id.Button01);

Button01.setOnClickListener(new ButtonListener());

new Thread(new myThread()).start();

}

class ButtonListener implements OnClickListener{

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

if(EditText02.getText()!=null&&EditText_001.getText()!=null&&EditText_002.getText()!=null){

b1=true;

}

if(CheckBox01.isChecked()){

s1=String.valueOf(Spinner_001.getSelectedItem())+"-"+String.valueOf(Spinner_002.getSelectedItem())+

"-"+String.valueOf(Spinner_011.getSelectedItem())+" "+String.valueOf(Spinner_012.getSelectedItem())+":"+

String.valueOf(EditText_001.getText())+":"+String.valueOf(EditText_002.getText());

s2=String.valueOf(EditText02.getText());

dd.put(s1, s2);

s1="";

s2="";

}else

{

s1=String.valueOf(Spinner_001.getSelectedItem())+"-"+String.valueOf(Spinner_002.getSelectedItem())+

"-"+String.valueOf(Spinner_011.getSelectedItem())+" "+String.valueOf(Spinner_012.getSelectedItem())+":"+

String.valueOf(EditText_001.getText())+":"+String.valueOf(EditText_002.getText());

s2=String.valueOf(EditText02.getText());

}

}

}

class myThread implements Runnable {

public void run() {

while (true) {

Message message = new Message();

message.what = 1;

mHandler.sendMessage(message);

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

Thread.currentThread().interrupt();

}

}

}

}

public void updateTitle() {

d1 = new Date();

time = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(d1);

TextView01.setText(time);

if(b1==true){

EditText01.setText("同学请输入完整!");

}

if(CheckBox01.isChecked()){

Set<String> set = dd.keySet();

for (String s : set) {

if (s.equals(time)) {

if (dd.get(s).equals("eat")) {

EditText01.setText("同学你该"+dd.get(s)+"了哦!");

}

if (dd.get(s).equals("xizao")) {

EditText01.setText("同学你该"+dd.get(s)+"了哦!");

}

if (dd.get(s).equals("sleep")) {

EditText01.setText("同学你该"+dd.get(s)+"了哦!");

}

}

}

}

else{

if (time.equals(s1)) {

EditText01.setText("同学你该"+s2+"了哦!");

}

}

}

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值