第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在android中動(dòng)態(tài)創(chuàng)建不同數(shù)量的textViews?

在android中動(dòng)態(tài)創(chuàng)建不同數(shù)量的textViews?

qq_遁去的一_1 2021-09-03 22:00:40
如何textView在運(yùn)行時(shí)根據(jù)變化創(chuàng)建不同數(shù)量的sinteger而不顯式實(shí)例化每個(gè)s的名稱textView?textView創(chuàng)建的每個(gè)都需要有一個(gè)參考,因?yàn)槲倚枰猳nClickListeners稍后應(yīng)用到每個(gè)。下面的代碼工作得很好,但是我沒(méi)有提到每個(gè)textViewfor onClickListeners:    private void buildUI(){    int variable_int = 3; //this variable changes at run-time, set to 3 for this examplefor(int i = 1; i <= variable_int; i++){            TextView textView = (TextView) findViewById(this);            textView.setVisibility(View.VISIBLE);            textView.setText("my textview");            linLayout.addView(textView);        }}我想到了一個(gè)解決方案,但java.lang.NumberFormatException在嘗試轉(zhuǎn)換R.id.box#為intfor時(shí)會(huì)產(chǎn)生異常findViewById():protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_profile);    //Create textViews in onCreate(). They are invisible by default    TextView box1 = (TextView) findViewById(R.id.box1);    TextView box2 = (TextView) findViewById(R.id.box2);    TextView box3 = (TextView) findViewById(R.id.box3);    TextView box4 = (TextView) findViewById(R.id.box4);    TextView box5 = (TextView) findViewById(R.id.box5);    buildUI(); }private void buildUI(){int variable_int = 3; //this variable changes at run-time, set to 3 for this examplefor(int i = 1; i <= variable_int; i++){            String x = "R.id.box"+i;            int xx = Integer.parseInt(x); //java.lang.NumberFormatException !            TextView textView = (TextView) findViewById(xx);            textView.setVisibility(View.VISIBLE);            textView.setText("my textview");            linLayout.addView(textView);        }}
查看完整描述

2 回答

?
白豬掌柜的

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊

您也許可以擁有一個(gè)TextViews數(shù)組,然后稍后對(duì)其進(jìn)行循環(huán)。


private TextView[] generatedViews;


private void generateViews(int size) {

    this.generatedViews = new TextView[size];


    for (int i = 0; i < size; ++i) {

        TextView tv = new TextView(this);

        ...

        generatedViews[i] = tv;

    }

}


private void assignListeners() {

    for (TextView tv : generatedViews) {

        tv.setOnClickListener(...);

    }

}


查看完整回答
反對(duì) 回復(fù) 2021-09-03
?
呼如林

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超3個(gè)贊

你也可以在創(chuàng)建 TextView 的同時(shí)創(chuàng)建一個(gè)事件


for(int i = 1; i <= variable_int; i++){


            TextView textView = (TextView) findViewById(this);


            textView.setVisibility(View.VISIBLE);

            textView.setText("my textview");


           **textView.setOnClickListener(....);**


            linLayout.addView(textView);


        }


查看完整回答
反對(duì) 回復(fù) 2021-09-03
  • 2 回答
  • 0 關(guān)注
  • 258 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)