如何通过单击按钮在TextView中显示Array项

时间:2019-04-04 19:47:35

标签: java android for-loop button switch-statement

我正在开发一个Android应用程序,并试图在TextView中显示一年中的所有月份。我需要每个月通过按钮单击显示一个。

这就是我一直认为它可以工作的方式。

    package com.flixarts.ar.englishnow;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    import org.w3c.dom.Text;

    public class saludos extends AppCompatActivity {
    private TextView espacioFrases;
    private Button next;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_saludos);
            monthsDisplay =(TextView)findViewById(R.id.espacioFrases);
            next = (Button)findViewById(R.id.next);
            next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //formularFrases();


        }
    });

}

String [] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September","October", "November", "December"};
String monthToDisplay = "";

public void showMonths(){

    for (int i=0 ; i<frases.length; i = i+1) {
        switch (i) {
            case 0 :
                monthToDisplay = months[i];
                monthDisplay.setText(monthToDisplay);
            case 1 :
                fraseamostrar = months[i];
                monthDisplay.setText(monthToDisplay);
            case 2 :
                fraseamostrar = months[i];
                monthDisplay.setText(monthToDisplay);
            case 3 :
                fraseamostrar = months[i];
                monthDisplay.setText(monthToDisplay);
            case 4 :
                fraseamostrar = months[i];
                monthDisplay.setText(monthToDisplay);
            case 5 :
                fraseamostrar = months[i];
                monthDisplay.setText(monthToDisplay);
            case 6 :
                fraseamostrar = months[i];
                monthDisplay.setText(monthToDisplay);

        }


    }




}

这里的问题是只显示第一个月的第一个月,因为每次我调用showMonths()方法时,变量“ i”的值都是0,所以总是显示第一个月。 我想保存变量i的值并加一(i ++),以便可以显示所有月份,但是每次调用函数i都会被初始化为0。

3 个答案:

答案 0 :(得分:0)

保存当前索引

String [] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September","October", "November", "December"};
int currentMonthIdx = 0;

此后,您可以在每次单击按钮时调用此功能:

public void onButtonClick() {
    currentMonthIdx = (currentMonthIdx + 1) % months.length;
    monthsDisplay.setText(months[currentMonthIdx]);
}

别忘了在启动时显示第一个月:

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_saludos);
        monthsDisplay =(TextView)findViewById(R.id.espacioFrases);
        monthsDisplay.setText(months[0]);
}

答案 1 :(得分:0)

public void showMonths(){

    for (int i=0 ; i<frases.length; i = i+1) {
        switch (i) {
            case 0 :
                monthToDisplay = months[i];
                monthDisplay.setText(monthToDisplay);
        break;
            case 1 :
                fraseamostrar = months[i];
                monthDisplay.setText(monthToDisplay);
        break;
            case 2 :
                fraseamostrar = months[i];
                monthDisplay.setText(monthToDisplay);
        break;
            case 3 :
                fraseamostrar = months[i];
                monthDisplay.setText(monthToDisplay);
        break;
            case 4 :
                fraseamostrar = months[i];
                monthDisplay.setText(monthToDisplay);
        break;
            case 5 :
                fraseamostrar = months[i];
                monthDisplay.setText(monthToDisplay);
        break;
            case 6 :
                fraseamostrar = months[i];
                monthDisplay.setText(monthToDisplay);
        break;

        }

答案 2 :(得分:0)

如果您需要为每次点击显示下个月,请尝试以下代码。

def test_async_who():
    res = async_who(callback)
    assert res == 'Bob'