【问题标题】:Android: array of textviewsAndroid:文本视图数组
【发布时间】:2011-10-08 04:27:45
【问题描述】:

我正在制作一个应用程序,我想在其中从字符串数组更改 textviews 的文本。 为此,我需要制作 textviews 数组。如何做到这一点?谁能帮我解决这个问题

【问题讨论】:

    标签: android textview


    【解决方案1】:

    您可以像这样创建 TextView:

    int textViewCount = 10;
    
    TextView[] textViewArray = new TextView[textViewCount];
    
    for(int i = 0; i < textViewCount; i++) {
       textViewArray[i] = new TextView(this);
    }
    

    【讨论】:

      【解决方案2】:

      如果你想要大量的文本视图,在这种情况下避免 OutofBound 异常使用以下代码

      LinearLayout parent = new LinearLayout(this);
              TextView textView;
              for( i = 0; i < count; i++) {
                  textView = new TextView(this);
                  textView.setTag(""+i);// setting tag with index i
                  parent.addView(textView);
              }
              int len=parent.getChildCount();
              int j = 0;
              int requiredPosition = 5;
              while(j<len) {
                  TextView tempTextView =((TextView)parent.getChildAt(i)); 
                  if( tempTextView.getTag().equals(""+requiredPosition)){
                      //Perform required operation
                      //tempTextView.setText("");
                  }
                  j++;
              }
      

      【讨论】:

        【解决方案3】:

        可能对你有用,我使用按钮数组,所以我在猜测 textview 的工作方式:

        TextView[ ][ ]  _txt;   
        
        _txt = new TextView[_dimension][_dimension]; // _dimension = 5 what you want
        _txt[0][0] = (TextView) findViewById(R.id.text1);
        _txt[0][1] = (TextView) findViewById(R.id.text2);
        

        还有更多...

        【讨论】:

        • 这是我的要求,所以你可以使用单个数组而不是 2d
        【解决方案4】:

        您可以通过以下方式实现:

        int textvwCount = 20;//number of textview you want to use in an array
        
        TextView[] arrTxtView = new TextView[textvwCount ]; // declare and assign array length of text views.
        
        for(int i = 0; i < textViewCount; i++) { // iterate over all array items and assign them text.
          Textview txtCnt = new TextView(this);
        txtCnt .settext(i);
         textViewArray[i] =txtCnt ;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-10-19
          • 2014-05-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-26
          • 1970-01-01
          相关资源
          最近更新 更多