按钮点击随机文字 [英] Random text on button click

查看:277
本文介绍了按钮点击随机文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想以随机顺序中显示,当我preSS一个按钮大约150不同的文本。我每次preSS的按钮时间一个新的。我想通了这个code:

I have around 150 different texts that I want to be shown in random order when I press a Button. A new one every time I press the Button. I have figured out this code:

   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;
   }
}
}   

我可以得到它链接到按钮。任何人都知道如何做到这一点?

I can get it to linked to a Button. Anyone know how to do this?

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:

}
}

} }

我仍然得到错误,在这里我把&LT; ---- X我究竟做错了什么?

I Still get errors where i put the "<----X" what am i doing wrong?

推荐答案

您onClickListener没有从中调用上下文 findViewById()。我可能会改用您的活动实现了点击监听器的设计模式。阅读的http://android-developers.blogspot.com/2009/10/ui-framework-changes-in-android-16.html并搜索更容易点击听众。

Your onClickListener has no context from which to call findViewById(). I would probably instead use the design pattern where your activity implements the click listener. Read http://android-developers.blogspot.com/2009/10/ui-framework-changes-in-android-16.html and search for "Easier click listeners".

这篇关于按钮点击随机文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆