展会信息港展会大全

Android开发教程用Random获取随机数
来源:互联网   发布日期:2016-01-14 12:24:14   浏览:2309次  

导读:一个随机数生成器,在首页不断变化,可以设置范围。random.javapackagezhang.random;importandroid.app.Activity;importandroid.app.AlertDialog;importandroid.content.Intent;importandroid.os.Bundle;importa......

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

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

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

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>

设置布局

<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

<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 随机数

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港