添加 TextView,当按钮被按下时 Kotlin
Add TextView, when button is pushed Kotlin
我不明白为什么不是这么简单...
// Add field
val LineLayoutInfo = findViewById<View>(R.id.LineLayoutInfo) as LinearLayout
val Add_Field = findViewById<View>(R.id.buttonAddField) as Button
Add_Field.setOnClickListener{
val tv1 = TextView(this@MainActivity)
tv1.text = "Show Up"
val t = TextView(applicationContext)
LineLayoutInfo.addView(tv1)
}
我只想在当前的 LinearLayout 中创建一个新的 TextView。这部分代码是用 Kotlin 编写的,并存储在 OnCreate() 中。可能是因为我需要刷新 activity?
按下按钮时得到的输出如下所示。
D/HwAppInnerBoostImpl: asyncReportData com.xxx.httpposter,2,1,1,0 interval=83
I/ViewRootImpl: jank_removeInvalidNode all the node in jank list is out of time
V/AudioManager: playSoundEffect effectType: 0
V/AudioManager: querySoundEffectsEnabled...
D/HwAppInnerBoostImpl: asyncReportData com.xxx.httpposter,2,1,2,0 interval=353
你可以试试这样的
private LinearLayout mLayout;
private Button mButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLayout = (LinearLayout) findViewById(R.id.linearLayout);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(onClick());
TextView textView = new TextView(this);
textView.setText("New text");
}
private OnClickListener onClick() {
return new OnClickListener() {
@Override
public void onClick(View v) {
mLayout.addView(createNewTextView("New Text"));
}
};
}
private TextView createNewTextView(String text) {
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText("New text: " + text);
return textView;
}
在这里,您可以只创建 TextView 并在程序 onCreate()
之后的 MainActivity
上将其可见性设置为 INVISIBLE
,然后在单击按钮时将其设置为 VISIBLE
.你可以无限地这样做!
我不明白为什么不是这么简单...
// Add field
val LineLayoutInfo = findViewById<View>(R.id.LineLayoutInfo) as LinearLayout
val Add_Field = findViewById<View>(R.id.buttonAddField) as Button
Add_Field.setOnClickListener{
val tv1 = TextView(this@MainActivity)
tv1.text = "Show Up"
val t = TextView(applicationContext)
LineLayoutInfo.addView(tv1)
}
我只想在当前的 LinearLayout 中创建一个新的 TextView。这部分代码是用 Kotlin 编写的,并存储在 OnCreate() 中。可能是因为我需要刷新 activity?
按下按钮时得到的输出如下所示。
D/HwAppInnerBoostImpl: asyncReportData com.xxx.httpposter,2,1,1,0 interval=83
I/ViewRootImpl: jank_removeInvalidNode all the node in jank list is out of time
V/AudioManager: playSoundEffect effectType: 0
V/AudioManager: querySoundEffectsEnabled...
D/HwAppInnerBoostImpl: asyncReportData com.xxx.httpposter,2,1,2,0 interval=353
你可以试试这样的
private LinearLayout mLayout;
private Button mButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLayout = (LinearLayout) findViewById(R.id.linearLayout);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(onClick());
TextView textView = new TextView(this);
textView.setText("New text");
}
private OnClickListener onClick() {
return new OnClickListener() {
@Override
public void onClick(View v) {
mLayout.addView(createNewTextView("New Text"));
}
};
}
private TextView createNewTextView(String text) {
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText("New text: " + text);
return textView;
}
在这里,您可以只创建 TextView 并在程序 onCreate()
之后的 MainActivity
上将其可见性设置为 INVISIBLE
,然后在单击按钮时将其设置为 VISIBLE
.你可以无限地这样做!