我在android studio中使用java和xml进行编码。我已经声明了三个数组,我已经声明了三个TextView,它们将显示每个数组中随机选择的字符串。
我是编程新手,我需要帮助:
到目前为止,这是我的代码:
public class MainActivity extends AppCompatActivity {
String[] listOne = new String[]{...};
String[] listTwo = new String[]{...};
String[] listThree = new String[]{...};
public TextView view1;
public TextView view2;
public TextView view3;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view1= (TextView)findViewById(R.id.textView1);
view2= (TextView)findViewById(R.id.textView2);
view3= (TextView)findViewById(R.id.textView3)
int listOneInt = new Random().nextInt(listOne.length);
int listTwoInt = new Random().nextInt(listTwo.length);
int listThreeInt = new Random().nextInt(listThree.length);;
}
public void generate(View view)
{
}
}
答案 0 :(得分:0)
我希望这有帮助(除了复制到剪贴板部分,抱歉不知道)
public class MainActivity extends AppCompatActivity {
String[] listOne = new String[]{"Hello","world","123"};
String[] listTwo = new String[]{"stack","over","flow"};
String[] listThree = new String[]{"do","it","yourself"};
public TextView view1;
public TextView view2;
public TextView view3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view1= (TextView)findViewById(R.id.textView1);
view2= (TextView)findViewById(R.id.textView2);
view3= (TextView)findViewById(R.id.textView3);
Random r = new Random();
final String randOne;
final String randTwo;
final String randThree;
int listOneInt = r.nextInt(listOne.length);
int listTwoInt = r.nextInt(listTwo.length);
int listThreeInt = r.nextInt(listThree.length);
randOne = listOne[listOneInt];
randTwo = listTwo[listTwoInt ];
randThree = listThree[listThreeInt ];
//Assuming you have a button with id assignRand
Button assignRandToView = (Button) findViewById(R.id.assignRand);
assignRandToView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
view1.setText(randOne);
view2.setText(randTwo);
view3.setText(randThree);
}
});
}
}