【问题标题】:Android: looping between text on button clickAndroid:在按钮点击文本之间循环
【发布时间】:2015-04-13 13:12:27
【问题描述】:

我想在循环中按下按钮时在 3 个不同的字符串之间切换。

我已将文本放入字符串数组中,但我不确定使用什么来循环数组。

    Button ranFw = (Button) findViewById(R.id.ranButFw);
    final TextView ranTx = (TextView) findViewById(R.id.ranText);



    String[] rangeTx = new String[2];
    rangeTx[0]="0 to 1700C";
    rangeTx[1]="32 to 3218F";
    rangeTx[2]="273.15 to 1973.15K";

    ranFw.setOnClickListener( new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

谢谢史蒂夫

更新:

ranFw = (Button) findViewById(R.id.ranButFw); final TextView ranTx = (TextView) findViewById(R.id.ranText);

     final String[] rangeTx = new String[3];//String[2] means index from 0 to 1
    rangeTx[0] = "0 to 1700C";
    rangeTx[1] = "32 to 3218F";
    rangeTx[2] = "273.15 to 1973.15K";
    ranFw.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (index) {
                case 0:
                    // if we are using index 0, set the text to index 1 text and change index to 1
                    index = 1;
                    ranTx.setText(rangeTx[index]);
                    break;
                case 1:
                    index = 2;
                    ranTx.setText(rangeTx[index]);
                    break;
                case 2:
                    index = 0;
                    ranTx.setText(rangeTx[index]);
                    break;

            }

        }

    });

没有错误,但似乎工作正常

【问题讨论】:

  • index 对你来说是什么?

标签: java android loops


【解决方案1】:

在这种情况下,您有多个选择,例如您有 For 和 While 循环、Switch Case 和 if else 语句。但最可行的解决方案是随机函数,它将生成一个介于 0 ,1 和 2 之间的随机数。您只需将该值设置为按钮。因此文本会发生变化。

final String[] rangeTx = new String[3];//String[2] means index from 0 to 1  
        rangeTx[0] = "0 to 1700C";
        rangeTx[1] = "32 to 3218F";
        rangeTx[2] = "273.15 to 1973.15K";
        ranFw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Random rand = new Random();//initializing function

                int randomNum = rand.nextInt((2 - 0) + 1) + 0;
                //max=2; min =0; because array has 3 length 0, 1, 2
               //rand.nextInt((max - min) + 1) + min; 

                 if(randomNum<3)
                    ranFw.setText(rangeTx[randomNum]);
                 else 
                    ranFw.setText(rangeTx [0]);

            }

        });

你也可以用 switch case 这个随机数。 希望这会有所帮助。快乐编码。

【讨论】:

    【解决方案2】:

    首先你的数组包含 3 个字符串,所以它是 new String[3]

    要迭代数组,您可以使用计数器。每次达到最后一个值时都重置它:

    final String[] rangeTx = new String[3];
    rangeTx[0]="0 to 1700C";
    rangeTx[1]="32 to 3218F";
    rangeTx[2]="273.15 to 1973.15K";
    
    ranFw.setOnClickListener( new View.OnClickListener() {
        private int mCounter;
        @Override
        public void onClick(View v) {
            Log.e("TAG", rangeTx[mCounter]);
            if (++mCounter == 3) mCounter = 0;
        }
    });
    

    【讨论】:

      【解决方案3】:

      一个简单的 switch/case 块可以很好地处理这个问题。

      // First, keep track of which index you are using
      int index = 2; // initialize it to 2 so that the first time u click the button you get the first text
      
      // loop through
      switch (index) {
          case 0:
              // if we are using index 0, set the text to index 1 text and change index to 1
              index = 1;
              ranTx.setText(rangeTx[index]);
          break;
          case 1:
              index = 2;
              ranTx.setText(rangeTx[index]);
          break;
          case 2:
              index = 0;
              ranTx.setText(rangeTx[index]);
          break;
      }
      

      switch/case 代码应该放在 onclick 监听器中,而索引应该放在外面,可能是全局的。然后,每次单击按钮时,文本都会更改为下一个。

      另外,你初始化你的字符串数组是错误的;应该是[3],而不是[2]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多