几天前在和社团和朋友们讨论安卓的项目,当时需要抽签决定谁做哪个模块,有没有现成的签来抽,于是就想到当场写一个生成随机数的小app,后来发现这个小应用还挺方便的,在很多场合都有用到,于是回来之后就细化了一下,现在发出来。
主题部分就是生成一个范围内的随机数,在Java中可以说是非常简单地程序了,只不过用安卓写要多加点东西。
MainActivity.java 文件:
package com.example.test;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
public int min;
public int max;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//这个是隐藏标题栏,我用了自定义的标题栏
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
Button button1 = (Button) findViewById(R.id.button);
ImageButton button2 = (ImageButton) findViewById(R.id.button2_title);
final TextView textView = (TextView) findViewById(R.id.text);
final EditText editText1 = (EditText) findViewById(R.id.edit1);
final EditText editText2 = (EditText) findViewById(R.id.edit2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//生成一个范围内的随机数
//范围的最小值
min = Integer.parseInt(editText1.getText().toString());
//范围的最大值
max = Integer.parseInt(editText2.getText().toString());
long t = System.currentTimeMillis(); 获取当前时间的毫秒数作为随机数种子
Random r1 = new Random(t);
int x = Math.abs(r1.nextInt((max - min + 1)) + min);
// int x = r1.nextInt(20);
textView.setText(String.valueOf(x));
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SetActivity.class);
startActivity(intent);
}
});
}
}
activity_main.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_gravity="center"
android:background="#ffffff"
android:layout_height="match_parent">
//这个是将我自己写的包括进来可以忽略
<include layout="@layout/title"/>
<TextView
android:id="@+id/text"
android:textColor="#ff0000"
android:textSize="50sp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="#c9b6b6" />
<EditText
android:id="@+id/edit1"
android:layout_weight="1"
android:hint="输入生成范围的最小值"
android:layout_width="match_parent"
android:numeric="integer"
android:ems="10"
android:layout_height="0dp" />
<EditText
android:id="@+id/edit2"
android:layout_weight="1"
android:hint="输入生成范围的最大值"
android:layout_width="match_parent"
android:numeric="integer"
android:ems="10"
android:layout_height="0dp" />
<Button
android:id="@+id/button"
android:text="click"
android:layout_weight="1"
android:background="#44ff00"
android:layout_gravity="top"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
好了主题部分就是这样,还有一些无关紧要的我就不写了
下面来看一下效果图:
更多推荐
Android安卓小程序-随机数生成器
发布评论