我正在编写一个具有Button
的Android应用程序,它调用SelfDestruct()
。还有一个TextView
,应该显示1
或2
,随机选择。但是,如果显示1
,则始终设置为1
,对于2
也是如此。它应该始终创建一个随机数。将TextField的值设置为随机数
这是我的代码,可能有人请帮助我实现这个...
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");
}
}
}
你的意思是你想随机第一次然后总是相同的值? –