我想創建一個按鈕點擊的文本視圖,這樣每次按下按鈕時都會創建一個新的文本視圖。在每個按鈕上創建一個文本視圖點擊
問題:我只得到單個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的不管我做您要在XML中添加的TextView
你做錯了,你只是更換舊一個 –
@Divyesh的TextView您能不能給一個解決方案嗎? –
等待一段時間 –