Android隨機數
⑴ android隨機數
我有個辦法:
首先你應該有10張數字圖片,然後你在代碼中寫個String數組【0.。。9】數組中的項對應相應的數字圖片,比如1圖片對應String【1】,這樣圖片上的數字 你就可以拿到4個String字元串了 然後把4個字元串用StringBuider拼接起來轉化成一個String字元串 和editext.gettext拿到的數據作比較,相等返回true 否則返回false。
java">String[]s={"0","1","2","3","4","5","6","7","8","9"};
//比如圖片展示的1052
StringBuilderstringBuilder=newStringBuilder();
stringBuilder.append(s[1]).append(s[0]).append(s[5]).append(s[2]);
StringtoString=stringBuilder.toString();
e=editText.getText().toString();
if(toString.equals(e)){
returntrue;
}else{
returnfalse;
}
⑵ android點擊按鈕讓它出現隨機數
在activity中放兩個控制項,一個Button,一個TextView,
給Button設置點擊監聽事件,
button.setOnClickListener(new OnClickListener(){
public void onClick(View v){
int num = Math.random()*10+20;//產生20-30的隨機數
textView.setText(String.valueOf(num));
}
}
⑶ android 為什麼我創建的臨時文件名里有一串隨機數
File.createTempFile()
這個方法內部實現的時候 就是添加了隨機數。防止文件重復,創建失敗。
⑷ android 怎樣生成隨機數
Randomrnd=newRandom();
intrndint=rnd.nextInt(10000);
生成0~10000之間的隨機數
⑸ android中怎麼產生一個隨機數
產生隨機數的話,可以直接使用random這個類,可以隨機生成數字的
⑹ 為什麼android中產生不了隨機數
不是android的問題,是你寫法有誤。Math.random()產生的大於等於0,小於1的隨機數,強轉int後變成0,乘以255還是0, 把(int)Math.random()*255改成(int)(Math.random()*255)就可以了。或者就像樓上所說的,用Random類的nextInt(MaxCount)方法也行
⑺ 如何讓android studio 上的 button 顯示的時候顯示隨機數
1、把手機屏幕分成上下。上下兩部分都採用Linearlayout方式布局 <LinearLayout> <LinearLayout> 上半部分 </LinearyLayout> <LinearLayout> 下半部分 </LinearyLayout> </LinearLayout>2、下半部分LinearLayout高度固定,上半部分LinearyLayout設置layout_weight權重,占滿剩餘屏幕空間 <LinearLayout> <LinearLayout android:layout_height="wrap_content" android:llayout_weight="1"> //設置高度自適應,並且權重為1 </LinearyLayout> <LinearLayout android:layout_height="50px"> //下半部分設置高度固定 </LinearyLayout> </LinearLayout>3、下半部分LinearLayout中添加按鈕,設置android:gravity右對齊。 <LinearLayout android:layout_height="50px" android:gravity="right"> //下半部分設置高度固定 <button andtoid:text="右下角按鈕"/> </LinearyLayout>
⑻ android如何產生隨機數
android產生隨機數的方法:此方法通過把當前時刻長整型數傳給Random對象,讓它產生的值隨著時間而變化。
[java] view plain
Strings = "";
Random ran =new Random(System.currentTimeMillis());
for (inti = 0; i < 10; i++) {
s =s + "\n" + ran.nextInt(100);
}
Toast.makeText(UiTestActivity.this,"Random: \n" + s, Toast.LENGTH_LONG).show();
⑼ android開發 生成隨機數 import java.util.Random; public c
不用改xml啊,你給textview設定一個id,代碼中通過findViewById方法獲取到這個id的view,然後調用setText方法就可以了
⑽ android如何實現產生個隨機數,並計算其中的兩個數
{
int[] x = new int[6];
StringBuilder b = new StringBuilder();
for (int i = 0; i < 10; i++) {
x[i] = (int) (Math.random() * 10);
b.append(x[i]);
}
b.insert(2, ".");
b.append("\n");
char[] c = b.toString().toCharArray();
b.append(c[2]+ "+"+ c[4]+ "="+ (Integer.parseInt(String.valueOf(c[2])) + Integer.parseInt(String.valueOf(c[4]))));
}
});
}
}