【问题标题】:change text use arraylist with onclicklistener to set next textView in arraylist by click the button通过单击按钮更改文本使用带有 onclicklistener 的 arraylist 来设置 arraylist 中的下一个 textView
【发布时间】:2019-09-22 10:42:38
【问题描述】:
NewText = (TextView)view.findViewById(R.id.mainn);
ChangeText = (ImageView)view.findViewById(R.id.ph);

// Create a list of words
ArrayList<String> words = new ArrayList<String>();
words.add("one");
words.add("two");
words.add("three");
words.add("four");
words.add("five");
words.add("six");
words.add("seven");
Words.add("eight");
words.add("nine");
words.add("ten");

ChangeText.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {

    //what code should I write here to change the text from one to two when I click the button??


      }


    });

【问题讨论】:

    标签: android android-studio arraylist textview onclicklistener


    【解决方案1】:

    如果要循环序列:

    if (index < words.size()) {
        NewText.setText( (CharSequence) words.get( index + 1 ) );
        index = index+1;
        return;
    }
    
    if (index == words.size()) {
        NewText.setText( (CharSequence) words.get( 0 ) );
        index = 0;
    }
    
    
    

    【讨论】:

    • ممكن رقم فونك او صفحة الفيس لو امكن
    • 到十点关门
    【解决方案2】:

    我已经有一段时间没有编写 Java 代码了,因为我现在正在编写 Flutter (dart),但我会尝试。

    private int index = 0;
    
    ...
    
    ChangeText.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View view) {
    
            if (index < words.size()) {
                NewText.setText( (CharSequence) words.get( index + 1 ) );
                index = index+1;
            }
    
        }
    
    });
    
    

    【讨论】:

    • 索引中的红色??
    • 您是否将int index = 0; 作为公共变量?
    • NewText.setText(words[index+1]);文字错误
    • تعرف تعمل سكرين شوت للصفحه كلها؟
    • طلب تعديل ليها عمل الكود مده NewText.setText( (CharSequence) words.get( index + 1 ) );
    【解决方案3】:

    ChangeText.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                index = index+1;
                if (index < ayaa.size()) {
                    NewText.setText( (CharSequence) ayaa.get( index  ) );
                    return;
                }
    
                if (index == ayaa.size()) {
                    NewText.setText( (CharSequence) ayaa.get( 0 ) );
                    index = 0;
                }
            }
    
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-27
      • 2023-01-22
      相关资源
      最近更新 更多