我是android开发的新手。我一直在阅读教程,学习和测试。
我正在创建自己的个人应用,我需要知道如何在A-Z和0-9之间生成一串字母和字母。
如果有人可以提供帮助,这将是一种贬义。
答案 0 :(得分:6)
public class Test extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linear = new LinearLayout(this);
linear.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
linear.setOrientation(LinearLayout.VERTICAL);
final TextView txt = new TextView(this);
txt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
100));
Button btn = new Button(this);
btn.setText("push");
txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
100));
btn.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String[] chars = {"a","b","c","d","e","x","y","z","0","1","2","8","9","10"};
txt.setText(chars[(int) (Math.random() * 10)]);
}
});
linear.addView(txt);
linear.addView(btn);
setContentView(linear);
}
}
答案 1 :(得分:4)
或许这样的事情:
import java.util.Random;
Random rnd = new Random();
int numLetters = 10;
String randomLetters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int n=0; n<numLetters; n++)
Log.i("Random letters" randomLetters.charAt(rnd.nextInt(randomLetters.length())));