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

问题描述 投票:0回答:3

我正在开发一个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);

        }


    }




}

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

java android for-loop button switch-statement
3个回答
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]);
}

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;

        }

0
投票

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

package onsource.com.onsourceinspector.data.api;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; 

public class saludos extends AppCompatActivity {
private TextView monthsDisplay;
int i = 0;
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) {
            showMonths();
        }
    });
}

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

public void showMonths() {
    if (i > 11)
        i = 0;      
    monthsDisplay.setText(months[i++]);
 }    
}
© www.soinside.com 2019 - 2024. All rights reserved.