随机数

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





random.java

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

    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. package zhang.random; 
  2.  
  3.  
  4. import android.app.Activity; 
  5. import android.content.Intent; 
  6. import android.os.Bundle; 
  7. import android.view.View; 
  8. import android.widget.Button; 
  9. import android.widget.EditText; 
  10.  
  11.  
  12. public class setMaxextends Activity{ 
  13.  
  14.         private EditText getMax; 
  15.         private Button ok; 
  16.         private Button cancle; 
  17.          
  18.     @Override 
  19.     protected void onCreate(Bundle savedInstanceState) { 
  20.         super.onCreate(savedInstanceState); 
  21.         setContentView(R.layout.max);    
  22.         getMax=(EditText)findViewById(R.id.set); 
  23.         ok=(Button)findViewById(R.id.okButton); 
  24.         cancle=(Button)findViewById(R.id.cancleButton); 
  25.          
  26.         ok.setOnClickListener(new View.OnClickListener() { 
  27.  
  28.             public void onClick(View arg0) { 
  29.                 int max =Integer.valueOf(getMax.getText().toString()); 
  30.                 Intent intent = new Intent(); 
  31.                 intent.putExtra("max",max); 
  32.                 intent.setClass(setMax.this, random.class); 
  33.                 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
  34.                 setMax.this.startActivity(intent); 
  35.                 setMax.this.finish(); 
  36.                  
  37.             } 
  38.         }); 
  39.         cancle.setOnClickListener(new View.OnClickListener() { 
  40.              
  41.             @Override 
  42.             public void onClick(View arg0) { 
  43.                 Intent intent2 = new Intent(); 
  44.                  setMax.this.setResult(RESULT_OK, intent2); 
  45.                    setMax.this.finish(); 
  46.             } 
  47.         }); 
  48.     } 
  49.  
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. <?xmlversion="1.0"encoding="utf-8"?> 
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent"> 
  6.   
  7. <TextView  
  8.     android:id="@+id/text" 
  9.     android:layout_width="wrap_content" 
  10.     android:layout_height="wrap_content" 
  11.     android:text="@string/textView"/> 
  12. <EditText  
  13.     android:id="@+id/set" 
  14.     android:layout_width="fill_parent" 
  15.     android:layout_height="wrap_content" 
  16.     android:maxLength="9" 
  17.     android:inputType="numberSigned"/> 
  18. <Button  
  19.         android:id="@+id/okButton" 
  20.         android:layout_width="fill_parent" 
  21.         android:layout_height="wrap_content" 
  22.         android:text="@string/ok" 
  23.         /> 
  24. <Button  
  25.         android:id="@+id/cancleButton" 
  26.         android:layout_width="fill_parent" 
  27.         android:layout_height="wrap_content" 
  28.         android:text="@string/cancle" 
  29.         /> 
  30. </LinearLayout> 
<?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. <?xmlversion="1.0"encoding="utf-8"?> 
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.      
  7.    
  8.     > 
  9. <TextView   
  10.  
  11.      
  12.     android:layout_width="fill_parent"  
  13.     android:layout_height="wrap_content"  
  14.     android:text="@string/hello" 
  15.     android:id="@+id/show" 
  16.     android:textSize="200dip" 
  17.     android:textStyle="bold" 
  18.     android:textColor="@color/white" 
  19.     android:gravity="center" 
  20.  
  21.     /> 
  22. <LinearLayoutandroid:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center"> 
  23. <Buttonandroid:layout_height="60dip"android:gravity="bottom"android:text="Start"android:id="@+id/start"android:layout_width="100dip"android:textSize="35dip"/> 
  24. <Button android:layout_height="60dip"android:gravity="bottom"android:text="Stop"android:id="@+id/stop"android:layout_width="100dip" android:textSize="38dip"></Button> 
  25.  
  26. </LinearLayout> 
  27.  
  28.  
  29.  
  30. </LinearLayout> 
<?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:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:id="@+id/show"
    android:textSize="200dip"
    android:textStyle="bold"
    android:textColor="@color/white"
    android:gravity="center"

    />
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center">
 <Button android:layout_height="60dip" android:gravity="bottom" android:text="Start" android:id="@+id/start" android:layout_width="100dip" android:textSize="35dip"/>
 <Button android:layout_height="60dip" android:gravity="bottom" android:text="Stop" android:id="@+id/stop" android:layout_width="100dip"  android:textSize="38dip"></Button>

</LinearLayout>



</LinearLayout>


String.xml

  1. <?xmlversion="1.0"encoding="utf-8"?> 
  2. <resources> 
  3.     <stringname="hello">00</string> 
  4.     <stringname="app_name">Random</string> 
  5.     <stringname="exit">退出</string> 
  6.     <stringname="about">关于</string> 
  7.     <colorname="white">#ffffff</color> 
  8.     <stringname="set">设置</string> 
  9.     <stringname="textView">输入最大值:</string> 
  10.     <stringname="ok">确定</string> 
  11.     <stringname="cancle">返回</string> 
  12.     <stringname="anthor">By:没落凄凉\nQQ:270615838</string> 
  13. </resources> 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值