Java 随机数到按钮
如何使用Java在Android studio中将随机数放入按钮?事先谢谢你 我想把随机数放在4x4按钮上 我尝试了此代码,但它在按钮中显示数组列表:Java 随机数到按钮,java,android,Java,Android,如何使用Java在Android studio中将随机数放入按钮?事先谢谢你 我想把随机数放在4x4按钮上 我尝试了此代码,但它在按钮中显示数组列表: private LinearLayout container; int[] ran={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Override protected void onCreate(@Nullabl
private LinearLayout container;
int[] ran={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
container = findViewById(R.id.container);
shuffleArray(ran);
for (int i = 0; i < container.getChildCount(); i++) {
LinearLayout row = (LinearLayout) container.getChildAt(i);
for (int j = 0; j < row.getChildCount(); j++) {
Button button = (Button) row.getChildAt(j);
button.setOnClickListener(this);
button.setText(Arrays.toString(ran));
button.setTextSize(24);
}
}
}
专用线性布局容器;
int[]ran={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
@RequiresApi(api=Build.VERSION\u code.LOLLIPOP)
@凌驾
创建时受保护的void(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
容器=findViewById(R.id.container);
沙弗雷(冉);
对于(int i=0;i
在没有实际布局背景的情况下,有点难理解您的问题。您可能需要编辑您的问题并向我们提供这些详细信息。随机字符串的长度应该是多少?
import java.util.*;
class IntasString{
public static void main(String args[]){
int min=1,max=15,result;
int myarr[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int range=(max-min)+1;
result=(int)(Math.random() * range) + min;
String indexValue=String.valueOf(myarr[result]);
System.out.println(indexValue);
}
}
Use this code according you and use variable indexValue to set text for Button