【问题标题】:How can I reference a text view that is created through code?如何引用通过代码创建的文本视图?
【发布时间】:2020-05-19 01:49:56
【问题描述】:
public TextView descriptionTextView(Context context, String text) {
        final ViewGroup.LayoutParams lparams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        final TextView textView = new EditText(context);
        textView.setLayoutParams(lparams);
        textView.setTextSize(10);
        textView.setTextColor(Color.rgb(0,0,0));
        textView.setText(" " + text + "");
        textView.setMaxEms(8);
        textView.setKeyListener(null);
        textView.setFocusableInTouchMode(false);
        textView.setEnabled(false);
        return textView;
    }

这是我为TextView 编写的代码。 我想从其他类或同一个类中引用它,但我找不到固定它的方法;就像我想根据输入更改值一样。

【问题讨论】:

  • 您必须使用此方法在运行时将TextView 添加到某个布局。因此,您可以在运行时通过读取该布局的子视图再次找到它,然后将 View 转换为 TextView 并更新数据。

标签: android dynamic reference generated


【解决方案1】:

TextView 一个 id,然后通过 id 访问 textView,如下所示 -

textView.setId(1);一样给id

public TextView descriptionTextView(Context context, String text) {
        final ViewGroup.LayoutParams lparams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        final TextView textView = new EditText(context);
        textView.setLayoutParams(lparams);
        textView.setTextSize(10);
        textView.setId(1);
        textView.setTextColor(Color.rgb(0,0,0));
        textView.setText(" " + text + "");
        textView.setMaxEms(8);
        textView.setKeyListener(null);
        textView.setFocusableInTouchMode(false);
        textView.setEnabled(false);
        return textView;
    }

假设你的 textView 布局是 LinearLayout。

您可以使用以下代码通过 id 获取 textView -

TextView tv = (TextView)view.findViewById(1); // view is LinearLayout object

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    这是动态创建TextView的代码

    LinearLayout layout = findViewById(R.id.linearLayout);
    
    TextView view = descriptionTextView(this, "Sample text");
    layout.addView(view);
    

    以下从相同/不同类调用它的方法:

    public TextView descriptionTextView(Context context, String text){
        ViewGroup.LayoutParams lparams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        final TextView textView = new TextView (context); //Change this to TextView
        textView.setLayoutParams(lparams);
        textView.setTextSize(10);
        textView.setTextColor(Color.rgb(0,0,0));
        textView.setText(" " + text + "");
        textView.setMaxEms(8);
        textView.setKeyListener(null);
        textView.setFocusableInTouchMode(false);
        textView.setEnabled(false);
        return textView;
    }
    

    【讨论】:

      【解决方案3】:

      在其他 XML 文件中创建视图不是 Android 中的首选方法。 最好的方法是在 XML 文件中制作这个 TextView 并使用它的 id 链接它。 像这样。

      <TextView
          android:id="@+id/textView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="TextView"
          />
      

      通过创建这样的对象,在您的类和活动中的任何地方进一步使用它。

       TextView textView = findViewById(R.id.textView);
          //to change value use this 
          textView.setText("Value you want to set");
          //to change its visibility
          textView.setVisibility(textView.GONE);
          //to get its string
          String value = textView.getText().toString();
      

      如果此方法没有帮助,并且您仍想在 Java 文件中创建视图,则将它们设为公共并超出函数边界,以使其随处可访问。

      【讨论】:

        猜你喜欢
        • 2019-07-08
        • 1970-01-01
        • 2015-05-27
        • 2015-11-23
        • 2018-08-29
        • 2014-11-14
        • 2020-09-24
        • 1970-01-01
        • 2022-08-18
        相关资源
        最近更新 更多