截图:
代码如下:
第三方依赖库地址:https://github.com/AndroidMsky/RandomTextView
第一步:添加仓库
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
第二步:
dependencies {
compile 'com.github.AndroidMsky:RandomTextView:v1.4'
}
第三步:编写xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_below="@+id/iv_back"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@mipmap/rule_bg">
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="@dimen/x40"
android:text="幸运选号"
android:textSize="@dimen/x16"
android:gravity="center"
android:background="#660108"/>
<ImageView
android:id="@+id/iv_back"
android:layout_width="@dimen/x30"
android:layout_height="@dimen/x30"
android:src="@mipmap/back"/>
<com.example.liangmutian.randomtextviewlibrary.RandomTextView
android:id="@+id/rt_v"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="0px"
android:letterSpacing="0.5"
android:text="00000"
android:textSize="@dimen/x50"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/x40"
android:onClick="start"
android:text="幸运选号"
android:background="@drawable/shape_dialog_text"
android:layout_below="@+id/rt_v"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
第四步: 编写Activity
public class RandomActivity extends Activity {
private RandomTextView mRandomTextView;
private int[] pianyiliang = new int[6];
private ImageView imageView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_random);
mRandomTextView = findViewById(R.id.rt_v);
imageView = findViewById(R.id.iv_back);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
pianyiliang[0] = 10;
pianyiliang[1] = 9;
pianyiliang[2] = 8;
pianyiliang[3] = 7;
pianyiliang[4] = 6;
pianyiliang[5] = 5;
mRandomTextView.setPianyilian(pianyiliang);
mRandomTextView.start();
}
public void start(View v) {
int[] num = new int [5];
Random random = new Random();
String str = "";
for (int i = 0 ;i <5 ; i++) {
int randomInt = random.nextInt(10);
num[i] = randomInt;
str += num[i] + "";
mRandomTextView.setText(str);
pianyiliang[0] = 7;
pianyiliang[1] = 6;
pianyiliang[2] = 12;
pianyiliang[3] = 8;
pianyiliang[4] = 18;
pianyiliang[5] = 10;
mRandomTextView.setMaxLine(40);
mRandomTextView.setPianyilian(pianyiliang);
mRandomTextView.start();
}
}
}
第五步: 点击运行Demo即可实现动态生成幸运数字的方法