我有大约150种不同的文本,当我按Button
时,我想按随机顺序显示。每次按Button
时都会有一个新的。我已经想通了这个代码:单击按钮上的随机文本
Random myRandom = new Random();
TextView textblondin = (TextView) findViewById(R.id.textblondin);
switch(myRandom.nextInt() %3) {
case 0:
textblondin.setText("Text 1");
break;
case 1:
textblondin.setText("Text 2");
break;
case 2:
textblondin.setText("Text 3");
break;
default:
break;
}
}
}
我可以得到它连接到一个Button
。有人知道怎么做吗?
public class Blondinskamt extends Activity { <----X
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.blondintext);
View myRandom = findViewById(R.id.myRandom);
myRandom.setOnClickListener(null);
myRandom.setOnClickListener(new View.OnClickListener() {
Random myRandom = new Random();
TextView textblondin = (TextView) findViewById(R.id.textblondin); <----X
switch(myRandom.nextInt() %3) {
case 0:
textblondin.setText("Skämt");
break;
case 1:
textblondin.setText("Roligt");
break;
case 2:
textblondin.setText("kul kul kul kul");
break;
default:
}}
我仍然得到错误,在这里我把 “< ---- X” 什么我做错了什么?
use Button myRandom = findViewById(R.id.myRandom); myRandom.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ //单击执行操作 } }); –