我正在尝试按下按钮创建文本视图,这样每次按下按钮都会创建一个新的文本视图。
问题:我只创建了单个textview,因为当我再次按下按钮时,什么都没有创建。
代码:
//In a class I defined a button
Button button;
//I set the button to on click listener
button.setOnClickListener(new View.OnClickListener{
@override
public void onClick(View view){
//now I want to create text view dynamically
TextView textview= new TextView(myactivity.this);
textview.setText("new text");
//the problem is that this method helps me make only one text I want to
// create text view each time the button pressed.
}
});
感谢
编辑:我没有忘记在布局中设置视图并给它参数,我无法找到问题,无论我做什么都生成一个textview
答案 0 :(得分:0)
首先添加要在xml中添加textview的布局:
<LinearLayout
android:id="@+id/anything"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
</LinearLayout>
现在进入活动:
LinearLayout lin;
lin = (LinearLayout) findViewById(R.id.anything);
lin.removeAllViews();
button.setOnClickListener(new View.OnClickListener{
@override
public void onClick(View view){
TextView txtName = new TextView(MainActivity.this);
txtName.setId(28);
txtName.setText("new text");
lin.addView(txtName);
}
});
答案 1 :(得分:0)
目前,您的代码只是用新的代码替换旧的textview。
要解决此问题,请从layout.xml中获取希望TextView显示的布局。
然后将TextView添加到该布局中,如下所示:
LinearLayout mLayout = (LinearLayout)findViewById(R.id.mLayout);
Button bn = (Button) findViewById(R.id.button2);
bn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
TextView tv1 = new TextView(MainActivity.this);
tv1.setText("Show Up");
mLayout.addView(tv1);
}
});
答案 2 :(得分:0)
创建xml,
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/llTextviews"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
</ScrollView>
在java类中,
LinearLayout layout;
LinearLayout linearlayout = new LinearLayout(ACTIVITY);
LayoutParams LLParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT_PARENT);
linearlayout .setLayoutParams(LLParams);
layout = (LinearLayout)findViewById(R.id.llTextviews);
button.setOnClickListener(new View.OnClickListener{
@override
public void onClick(View view){
layout.removeAllViews();
TextView txtName = new TextView(MainActivity.this);
txtName.setId(28);
txtName.setText("new text");
linearlayout.addView(txtName);
layout.addView(linearLayout);
}
});
答案 3 :(得分:0)
为了动态生成View Id,请使用表单API 17
喜欢这个
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText editText = new EditText(MainActivity.this);
editText.setId(editText.generateViewId());
editText.setHeight(50);
editText.setWidth(50);
ll.addView(editText);
}
private EditText editText(String hint) {
EditText editText = new EditText(this);
editText.setId(Integer.valueOf(hint));
editText.setHint(hint);
editTextList.add(editText);
return editText;
}