Android Random随机数

一个 随机数生成器,在首页不断变化,可以设置范围。

random.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package zhang.random;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class random extends Activity{
protected void onResume() {
super .onResume();
}
private Button start;
private Button stop;
private TextView show;
private Handler handler;
private Runnable update;
private int i;
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.random_main);
//layout file
start=(Button)findViewById(R.id.start);
stop=(Button)findViewById(R.id.stop);
show=(TextView)findViewById(R.id.show);
handler = new Handler();
update = new Runnable(){
public void run(){
Intent intent= getIntent();
int value = intent.getIntExtra( "max" , 100 );
i = Integer.valueOf(( int ) (Math.random()*value)); //获得一个<a title="随机数" href="http://www.android-study.com/jichuzhishi/84.html">随机数</a>
if (value <= 10 ) {
show.setTextSize( 280 ); // 1
}
else if (value > 10 && value <= 100 ) {
show.setTextSize( 200 ); //2
}
else if (value > 100 && value <= 1000 ) {
show.setTextSize( 170 ); //3
}
else if (value > 1000 && value <= 10000 ) {
show.setTextSize( 145 );
}
else {
show.setTextSize( 60 );
}
show.setText(i+ "" );
handler.postDelayed(update, 3 );
}
};
start.setOnClickListener( new View.OnClickListener() {
public void onClick(View arg0) {
handler.post(update);
start.setEnabled( false );
}
});
stop.setOnClickListener( new View.OnClickListener() {
public void onClick(View arg0) {
handler.removeCallbacks(update);
start.setEnabled( true );
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
menu.add( 0 , 1 , 1 ,R.string.set); //add menu-set
menu.add( 0 , 2 , 2 ,R.string.about); //add menu-about
menu.add( 0 , 3 , 3 ,R.string.exit); //add menu-exit
return super .onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == 3 ) { //OnClick set
finish();
}
else if (item.getItemId() == 2 ) { //OnClick about
AlertDialog.Builder dialog = new AlertDialog.Builder( this );
dialog.setTitle( "About" ).setMessage(R.string.anthor).show();
}
else { //onClick exit
Intent intent = new Intent();
intent.setClass(random. this ,setMax. class );
random. this .startActivity(intent);
}
return super .onOptionsItemSelected(item);
}
}

设置范围的Activity

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package zhang.random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class setMax extends Activity{
private EditText getMax;
private Button ok;
private Button cancle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.max);
getMax = (EditText)findViewById(R.id.set);
ok = (Button)findViewById(R.id.okButton);
cancle = (Button)findViewById(R.id.cancleButton);
ok.setOnClickListener( new View.OnClickListener() {
public void onClick(View arg0) {
int max = Integer.valueOf(getMax.getText().toString());
Intent intent = new Intent();
intent.putExtra( "max" ,max);
intent.setClass(setMax. this , random. class );
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
setMax. this .startActivity(intent);
setMax. this .finish();
}
});
cancle.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent2 = new Intent();
setMax. this .setResult(RESULT_OK, intent2);
setMax. this .finish();
}
});
}
}

主页布局

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent" >
< TextView
android:id = "@+id/text"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "@string/textView" />
< EditText
android:id = "@+id/set"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:maxLength = "9"
android:inputType = "numberSigned"
/>
< Button
android:id = "@+id/okButton"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:text = "@string/ok"
/>
< Button
android:id = "@+id/cancleButton"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:text = "@string/cancle"
/>
</ LinearLayout >

设置布局

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_height = "fill_parent"
android:layout_width = "fill_parent"
android:orientation = "vertical"
>
< TextView
android:gravity = "center"
android:id = "@+id/show"
android:layout_height = "wrap_content"
android:layout_width = "fill_parent"
android:text = "@string/hello"
android:textcolor = "@color/white"
android:textsize = "200dip"
android:textstyle = "bold"
/>
< LinearLayout
android:gravity = "center"
android:layout_height = "wrap_content"
android:layout_width = "fill_parent"
android:orientation = "horizontal"
>
< Button
android:gravity = "bottom"
android:id = "@+id/start"
android:layout_height = "60dip"
android:layout_width = "100dip"
android:text = "Start"
android:textsize = "35dip"
type = "submit"
/>
< Button
android:gravity = "bottom"
android:id = "@+id/stop"
android:layout_height = "60dip"
android:layout_width = "100dip"
android:text = "Stop"
android:textsize = "38dip"
type = "submit"
/>
</ LinearLayout >
</ LinearLayout >

String.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
< Resources >
< string name = "hello" >00</ string >
< string name = "app_name" >Random</ string >
< string name = "exit" >退出</ string >
< string name = "about" >关于</ string >
< color name = "white" >#ffffff</ color >
< string name = "set" >设置</ string >
< string name = "textView" >输入最大值:</ string >
< string name = "ok" >确定</ string >
< string name = "cancle" >返回</ string >
< string name = "anthor" >By:没落凄凉\nQQ:270615838</ string >
</ Resources >
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值