【问题标题】:How to implement an array of TextView objects in a for loop?如何在 for 循环中实现一组 TextView 对象?
【发布时间】:2018-01-28 03:23:39
【问题描述】:

我想通过使用将 TextView 对象 a、b、c、d、e 作为数组元素的 for 循环来最小化代码。对于 findViewById 和 setOnClickListener 实现。非常感谢此特定编码的任何可操作演练!^__^

  • 以下是我通常如何执行 TextView 实现。但是我 我厌倦了不必要地写这么多行。

    TextView a,b,c,d,e;

        a=(TextView)findViewById(R.id.A);
        b=(TextView)findViewById(R.id.B);
        c=(TextView)findViewById(R.id.C);
        d=(TextView)findViewById(R.id.D);
        e=(TextView)findViewById(R.id.E);
    
        a.setOnClickListener(this);
        b.setOnClickListener(this);
        c.setOnClickListener(this);
        d.setOnClickListener(this);
        e.setOnClickListener(this);
    
  • 我的问题是我是否可以使用循环来设置所有已经初始化的 TextView 对象调用 setOnClickListener() 没有任何麻烦 如下图:

    TextView a,b,c,d,e;
    
    a=(TextView)findViewById(R.id.A);
    b=(TextView)findViewById(R.id.B);
    c=(TextView)findViewById(R.id.C);
    d=(TextView)findViewById(R.id.D);
    e=(TextView)findViewById(R.id.E);
    

    TextView[] textViews = {a, b, c, d, e};

    for (int count = 0; count < textViews.length; count++) {
        textViews[count].setOnClickListener(this);
    }
    

**

  • 我只想知道如何初始化 TextView 对象 a,b,c,d,e 以同样的方式,我向他们展示了 setOnClickListener?像这样的,

    TextView[] textViews = {a, b, c, d, e};
    
    int[] textViewIds = {R.id.a, R.id.b, R.id.c, R.id.d, R.id.e};
    
    for (int count = 0; count < textViews.length; count++) {
        textViews[count] = (TextView)findViewById(textViewIds[count]);
    }
    

**

【问题讨论】:

  • 将 id 放入列表/数组并循环遍历它们?
  • 是的。我同意@bub

标签: android for-loop optimization textview


【解决方案1】:

我就是这样写的(我这里没有编译器,所以很抱歉出错)

ArrayList<TextView> textViews = new ArrayList<TextView>();

int[] tvIds = {R.id.A,R.id.B,R.id.C,R.id.D,R.id.E};

for(int index= 0; index<tvIds.length/* or .count forgot sorry*/; index++){   

 TextView tv = (TextView)findViewById(tvIds[index]));
 tv.setOnClickListener(this);

 textViews.add(tv);

}

或者你可以使用@Hank Moody 的回答,butterknife 非常简单。

【讨论】:

    【解决方案2】:

    我正在使用 ButterKnife 进行视图绑定,它有很好的方法来做你想做的事。

    首先你可以在List中绑定视图

    @BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
    List<TextView> nameViews;
    

    然后你可以用“Action”对这个列表应用不同的操作,像这样:

    static final ButterKnife.Action<View> SET_CLICK = new ButterKnife.Action<View>() {
    @Override
    public void apply(View view, int index) {
        view.setOnClickListener(....listener);
      }
    };
    

    然后应用这个动作

    ButterKnife.apply(nameViews, DISABLE);
    

    查看示例here

    UPD:如果您使用 Kotlin,请参阅 Kotter Knife

    【讨论】:

      【解决方案3】:

      //在类的开头声明视图对象为全局变量

      View v_home, v_earning, v_rating, v_account;
      

      //为视图创建视图数组,为视图ID创建int数组

      View[] views ={v_home, v_earning, v_rating, v_account};
      int[] viewsID ={R.id.v_home, R.id.v_earning, R.id.v_rating, R.id.v_account};
      

      //然后for循环里面findViewById和setOnClickListener

      for (int v=0;v<views.length;v++){
              views[v] = findViewById(viewsID[v]);
              views[v].setOnClickListener(this);
          }
      

      【讨论】:

        猜你喜欢
        • 2014-02-15
        • 2021-10-28
        • 1970-01-01
        • 2022-01-10
        • 2012-04-16
        • 2021-09-26
        • 1970-01-01
        • 2022-08-11
        • 2019-11-15
        相关资源
        最近更新 更多