您的位置:首页 > 移动开发 > Android开发

Android Random随机数

2013-10-28 08:44 357 查看
一个随机数生成器,在首页不断变化,可以设置范围。
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="#">随机数</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
31
<?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
42
<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
13
<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>
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  random android java ios