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

android-Random随机数

2011-08-04 21:10 357 查看
一个随机数生成器,在首页不断变化,可以设置范围



random.java

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

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();
}
});
}

}


主页布局

<?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>


设置布局

<?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

<?xml version="1.0" encoding="utf-8"?>
<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: