实现了一个简单的button按键消息处理,一个简单的textview显示,以及一个可编辑输入框
实测可用
Android 5.0.1 API(21) + Nexus One摸拟器及Android 5.1.1SDK真机均可运行
xml layout code
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
java code
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.example.showsystime_randomnum_calc;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
/*
private int value1 = 10; //OK 20
private int value2 = 10;//OK 20
private int result = 0;//OK 20
int i = 0;
*/
private TextView textView;
private Button RandomStartBtn;
private Button RandomStopBtn;
private Button SysTimeStartBtn;
private Button SysTimeStopBtn;
private Boolean begined = false;
private Boolean StopSystemTime = true;
private Timer timer;
private Handler handler;
private TextView mTime;
private static final int msgKey1 = 2;
private TimeThread mTimeThread = null;
private EditText EDTpower;
private EditText EDTdata;
private TextView TVresult;
private Button CaclBtn;
private int power;
private int data;
private int result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
final TextView showtext = (TextView)findViewById(R.id.textView1);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//showtext.setText("01:10\n"); //OK
result = 0;
for(i=0; i<80; i++) //OK 800但是不会刷新,百度说需要用handler,timer,thread
{
//result = value1+value2; //OK 20
//showtext.setText(""+result+""); //OK 20
result += value1; //OK 20
showtext.setText(""+result+""); //OK 20
}
}
});
Button button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//int i = 10;
showtext.setText("00:00\n");
//showtext.setText(i);
}
});
*/
init();
handler = new Handler()
{
public void handleMessage(Message msg)
{
super.handleMessage(msg);
if(begined == true)
{
textView.setText("" +msg.what);
}
else if(begined == false)
{
textView.setText("" +msg.what);
timer.cancel(); //Timer停止
}
}
};
}
void init()
{
textView = (TextView)this.findViewById(R.id.textView1);
mTime = (TextView) findViewById(R.id.textView4);
RandomStartBtn = (Button)this.findViewById(R.id.button1);
RandomStopBtn = (Button)this.findViewById(R.id.button2);
SysTimeStartBtn = (Button)this.findViewById(R.id.button3);
SysTimeStopBtn = (Button)this.findViewById(R.id.button4);
TVresult=(TextView)findViewById(R.id.textView7);
EDTpower = (EditText) findViewById(R.id.edit1);
EDTdata=(EditText)findViewById(R.id.edit2);
CaclBtn = (Button)this.findViewById(R.id.button5);
RandomStartBtn.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
begined = true;
RandomStartBtn.setEnabled(false);
RandomStopBtn.setEnabled(true);
timer = new Timer();
timer.schedule(new TimerTask()
{
public void run()
{
// TODO Auto-generated method stub
Message message = new Message();
message.what = (int) (Math.random() * 999 + 100);
handler.sendMessage(message);
}
}, 1000, 500); //从100ms即0.1s开始,500ms为数字改变周期
}
});
RandomStopBtn.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
begined = false;
RandomStartBtn.setEnabled(true);
RandomStopBtn.setEnabled(false);
}
});
SysTimeStartBtn.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
SysTimeStartBtn.setEnabled(false);
SysTimeStopBtn.setEnabled(true);
if(mTimeThread == null)
{
mTimeThread = new TimeThread();
mTimeThread.start();
}
StopSystemTime = true;
}
});
SysTimeStopBtn.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
SysTimeStartBtn.setEnabled(true);
SysTimeStopBtn.setEnabled(false);
//mTime.destroy();
StopSystemTime = false;
}
});
CaclBtn.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
power =Integer.parseInt(EDTpower.getText().toString()) ;
data = Integer.parseInt(EDTdata.getText().toString());
result = 1;
while(power >= 1)
{
result = result*2;
power --;
}
result =result + data;
TVresult.setText(""+result+"");
}
});
}
public class TimeThread extends Thread {
@Override
public void run () {
do {
try {
Thread.sleep(1000);
Message msg = new Message();
msg.what = msgKey1;
mHandler.sendMessage(msg);
}
catch (InterruptedException e) {
e.printStackTrace();
}
} while(true);
}
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage (Message msg) {
super.handleMessage(msg);
if(StopSystemTime == true)
{
switch (msg.what) {
case msgKey1:
long sysTime = System.currentTimeMillis();
CharSequence sysTimeStr = DateFormat.format("hh:mm:ss", sysTime);
mTime.setText(sysTimeStr);
break;
default:
break;
}
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}