Android:点击按钮时在文本之间循环

Android: looping between text on button click

我想在循环按下按钮时在 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); 最终 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;

            }

        }

    });

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

一个简单的 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]

首先你的数组包含 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;
    }
});

在这种情况下,您有不止一种选择,例如您有 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 这个随机数。 希望这会有所帮助。快乐的编码。