【发布时间】:2012-06-05 15:45:01
【问题描述】:
我正在编写一个具有Button 调用SelfDestruct() 的Android 应用程序。还有一个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");
}
}
}
【问题讨论】:
-
你的意思是你希望它第一次随机然后总是相同的值?
标签: java android random if-statement