非常羡慕那些语文成绩好的,对于我这种读书的时候不会写作文的人来说,写博客真的是一件很痛苦的事,有时候甚至怀疑自己是否有语言表达障碍!不过这阻止不了我想要去进步的想法,看了这么多大神的博客,我认为写博客能让我进步,所以就来献丑了!
第一次写博客,写不出太牛逼的东西,就拿自己刚刚学习Android时使自己感兴趣的一个Demo来复习一下自己以前学过的基础知识。
下面直接上代码:
package com.example.myssq;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tv_red,tv_blue;
Button bt_getone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
setViews();
//初始化时,默认机选一注
setTextssq();
}
private void setViews() {
tv_red= (TextView) findViewById(R.id.tv_red);
tv_blue= (TextView) findViewById(R.id.tv_blue);
bt_getone= (Button) findViewById(R.id.bt_getone);
bt_getone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setTextssq();
}
});
}
//显示机选的双色球
private void setTextssq() {
tv_red.setText(ShuangSeQiu.getRed());
tv_blue.setText(ShuangSeQiu.getBlue());
}
}
以上是MainActivity代码
下面是布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myssq.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/linearLayout">
<TextView
android:id="@+id/tv_red"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@android:color/holo_red_dark"
android:layout_weight="6"
android:text="Hello World!" />
<TextView
android:id="@+id/tv_blue"
android:layout_width="0dp"
android:gravity="center"
android:textColor="@android:color/holo_blue_dark"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="blue" />
</LinearLayout>
<Button
android:id="@+id/bt_getone"
android:layout_width="wrap_content"
android:text="机选一注"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout"
android:layout_alignParentStart="true" />
</RelativeLayout>
就是两个textview,加一个button
再下面就是我们提取出来的获取双色球数字的类:
package com.example.myssq;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Random;
/**
* Created by Administrator on 2016/4/17.
*/
public class ShuangSeQiu {
static String[] r =
{
"01", "02", "03", "04", "05", "06",
"07", "08", "09", "10", "11", "12",
"13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29", "30",
"31", "32", "33"
};
static String[] b =
{
"01", "02", "03", "04", "05", "06",
"07", "08", "09", "10", "11", "12",
"13", "14", "15", "16"
};
//获取红球的字符串
public static String getRed() {
//设置标记数组,标记当前位子的数字是否已选
boolean[] flag = new boolean[33];
//创建目标数组,放入取出的红球
String[] red = new String[6];
for (int i = 0; i < red.length; i++) {
int index;
do {
index = new Random().nextInt(33);
} while (flag[index]);
red[i] = r[index];
flag[index] = true;
}
//给red排序
Arrays.sort(red);
return Arrays.toString(red);
}
//获取篮球的字符串
public static String getBlue() {
String blue = b[new Random().nextInt(16)];
return blue;
}
}
这就简单的实现了我们点击一次button,就获取一次随机的双色球数据,当然后期我们也可以进行优化,可以用图片来显示数字,还可以在后面加一个机选5注的按钮,在下面加一个listview显示多条之类的。后期再慢慢根据自己的喜好添加!!