简体   繁体   English

我在测试中有5个选择。 我有一个csv数据库。 当我按下其中一个按钮时,我希望按钮上的“文本”更改为下一个标题

[英]I have 5 choices in a test. I have a csv database. When I press on one of the buttons, I want the Text on the button to change to the next header

I have 5 choices in a test. 我在测试中有5个选择。 I have a csv database which I read the headers and answers from. 我有一个csv数据库,可从中读取标题和答案。 When I press on one of the buttons, I want the Text on the button to change to the next header. 当我按下其中一个按钮时,我希望按钮上的“文本”更改为下一个标题。 I want to use a "for" loop for that. 我想为此使用“ for”循环。 How will I keep the same layout, but change the text on the buttons in this for loop? 我如何保持相同的布局,但是在此for循环中更改按钮上的文本?

public class MainActivity extends AppCompatActivity {

int say,may=0;

Button a,b,c,d,e;
private List<WeatherSample> weatherSamples=new ArrayList<>();
String[][] deneme=new String[20][7];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Button asikki = (Button) findViewById(R.id.asikki);
    final Button bsikki = (Button) findViewById(R.id.bsikki);
    final Button csikki = (Button) findViewById(R.id.csikki);
    final Button dsikki = (Button) findViewById(R.id.dsikki);
    final Button esikki = (Button) findViewById(R.id.esikki);

    String line = "";
    a = (Button) findViewById(R.id.asikki);
    b = (Button) findViewById(R.id.bsikki);
    c = (Button) findViewById(R.id.csikki);
    d = (Button) findViewById(R.id.dsikki);
    e = (Button) findViewById(R.id.esikki);

    InputStream is = getResources().openRawResource(R.raw.data);

    BufferedReader reader = new BufferedReader(
            new InputStreamReader(is, Charset.forName("UTF-8"))
    );

    try {
        // step over header line
        reader.readLine();

        while ((line = reader.readLine()) != null) {

            may++;
            String sira = Integer.toString(may);
            String[] tokens = line.split(",");
            WeatherSample sample = new WeatherSample();

            deneme[may][0] = tokens[0];
            deneme[may][1] = tokens[1];
            deneme[may][2] = tokens[2];
            deneme[may][3] = tokens[3];
            deneme[may][4] = tokens[4];
            deneme[may][5] = tokens[5];
            deneme[may][6] = tokens[6];
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    for (int say=0;say<10;say++){
        a.setText("A) " + deneme[1][0]);
        b.setText("B) " + deneme[1][1]);
        c.setText("C) " + deneme[1][2]);
        d.setText("D) " + deneme[1][3]);
        e.setText("E) " + deneme[1][4]);

        asikki.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                asikki.setText("bilgin");
                asikki.setBackgroundColor(Color.BLACK);
                asikki.setTextColor(Color.WHITE);
            }
        });

        bsikki.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                bsikki.setText("bilgin");
                bsikki.setBackgroundColor(Color.BLACK);
                bsikki.setTextColor(Color.WHITE);
            }
        });

        csikki.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                csikki.setText("bilgin");
                csikki.setBackgroundColor(Color.BLACK);
                csikki.setTextColor(Color.WHITE);
            }
        });

        dsikki.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dsikki.setText("bilgin");
                dsikki.setBackgroundColor(Color.BLACK);
                dsikki.setTextColor(Color.WHITE);
            }
        });

        esikki.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                esikki.setText("bilgin");
                esikki.setBackgroundColor(Color.BLACK);
                esikki.setTextColor(Color.WHITE);
            }
        });

     }

}

for loop is something to be executed immediately, it doesn't wait, you only need the listener for循环是立即执行的操作,它不等待,您只需要侦听器

    asikki.setText(deneme[0][0]);
    asikki.setBackgroundColor(Color.BLACK);
    asikki.setTextColor(Color.WHITE);

    asikki.setOnClickListener(new View.OnClickListener() {
        int say = 1;
        public void onClick(View v) {
                // TODO check say isn't out of bound
            asikki.setText("A) " +deneme[say++][0]);
        }
    });

Thanks to @Happy, Now I have the following lines in my code: 感谢@Happy,现在我的代码中包含以下几行:

int say = 1;
asikki.setText(deneme[0][0]);
asikki.setBackgroundColor(Color.BLACK);
asikki.setTextColor(Color.WHITE);

asikki.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        say++;
        asikki.setText("A) " +deneme[say][1]);
        bsikki.setText("B) " +deneme[say][2]);
        csikki.setText("C) " +deneme[say][3]);
        dsikki.setText("D) " +deneme[say][4]);
        esikki.setText("E) " +deneme[say][5]);
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我有一个 Jtable,其中一列中有一个按钮,我想在按下按钮时访问第一列中的值 - I have a Jtable with a button in one of the columns and I want to access the value in the 1st column when I press the button 当我按下1按钮时,我希望所有其他按钮更改状态 - When I press 1 button, I want all other buttons to change states 当我按“ i”时,我有一个清单,但是我想知道如果我按“ i”时该如何清除它 - I have an inventory drawn out when i press “i” but I want to know how to get rid of it if i press “i” agian 当我按下按钮时,如何更改JTextArea中的文本? - How do I change the text in a JTextArea when I press a button? 当我按下点按钮时,我希望该点按钮在输入 1 中只按下一次? - when i press dot button i want that dot button press only one time in input 1? 我想要一个单选按钮,当检查应该增加计数,这里计数不增加,如果我有一行 - I want to a radio button when checked should increment count ,here count is not incrementting if i have one row 我有 5 个按钮我想更改按钮的 mediaPlayer 源 fonClick 怎么做? - I have 5 buttons I want to change mediaPlayer source fonClick of the buttons how to do that? 我有两种方法需要使用JUnit测试进行测试。 这是代码 - I have two methods that need to be tested using the JUnit test. Here are the codes 我正在尝试每小时进行一次通知,但是想要从数据库中填充,我有工作代码,但下次通知不会长达一个小时 - I'm trying to do the notification every hour but want to populate from database, I have working code but not up to one hour for next notification 我已经从sqlite数据库中填充了自定义列表视图。 列表视图自定义行包含一个imageview,textview和button。 - I have populated my custom list view from sqlite database. List view custom row contains one imageview ,textview and button.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM