Android实时添加按钮
Android realtime add button
我需要一个按钮,单击该按钮后会在其下方添加一个 TextView。此外,在这种情况下,不能使 TextView 不可见并在单击时出现,它必须添加 TextView。正在寻找这个,但找不到适合我需要的东西。
在按钮上设置一个侦听器,每次单击都会创建一个新的 TextView 并将其添加到布局中。
final LinearLayout theLayout = (LinearLayout)findViewById(R.id.thelayout);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView tv = new TextView(MainActivity.this);
tv.setText("Hello");
tv.setTextColor(Color.BLACK);
theLayout.addView(tv);
}
});
更新:在上方添加了文字颜色
我需要一个按钮,单击该按钮后会在其下方添加一个 TextView。此外,在这种情况下,不能使 TextView 不可见并在单击时出现,它必须添加 TextView。正在寻找这个,但找不到适合我需要的东西。
在按钮上设置一个侦听器,每次单击都会创建一个新的 TextView 并将其添加到布局中。
final LinearLayout theLayout = (LinearLayout)findViewById(R.id.thelayout);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView tv = new TextView(MainActivity.this);
tv.setText("Hello");
tv.setTextColor(Color.BLACK);
theLayout.addView(tv);
}
});
更新:在上方添加了文字颜色