我如何声明 Textview 变量

How I declare Textview variable

我的问题是如何在 javacode 中声明 Textview 变量?

我的代码在这里:

TextView tv1 = new TextView(MainActivity.this);

我想在此函数中使用 tv1:

build.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

}

我尝试了这个代码:tv1 = new TextView(MainActivity.this) 但我遇到了这个问题:

Multiple markers at this line - Syntax error on token "new", delete this token - TextView cannot be resolved to a variable - The final local variable tv1 cannot be assigned, since it is defined in an enclosing type

findViewById()..它响了吗?

您需要在内容视图布局中声明文本视图并使用它,方法是使用 findViewById(R.id.view_id );

分配它

要将文本视图添加到布局,请遵循此

LinearLayout linear = (LinearLayout) findViewById(R.id.linearlayout2);
TextView tv = new TextView(this);
tv.setText("Text here");
tv.setId(id);
linear.addView(tv);