我正在编写一个具有Buttonwhich 调用的 Android 应用程序SelfDestruct()。还有一个TextView应该显示1或2随机选择的。但是,如果它显示1,总是1会被设置,对于2。它应该始终创建一个随机数。
这是我的代码,有人可以帮我实现这个...
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void SelfDestruct(View View)
{
TextView tx= (TextView) findViewById(R.id.text);
Random r = new Random();
int x=r.nextInt(2-1) + 1;
if(x==1)
{
tx.setText("1");
}
else if(x==2)
{
tx.setText("2");
}
}
}