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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用按鈕 onclick 事件在運行時添加按鈕

使用按鈕 onclick 事件在運行時添加按鈕

千萬里不及你 2021-06-24 18:19:46
我正在制作一個游戲,其中平臺將在用戶清除的每個級別中添加新按鈕。但我無法在運行時在項目中添加新的 xml 標(biāo)記。我有點不知道要使用什么或如何實施。    run = (Button)findViewById(R.id.btnRunChallengeMode);    run.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View view) {            Intent newForm = new Intent(Form2.this,Form2.class);            buttonPanel = (LinearLayout)findViewById(R.id.LinearButtonPanel);            Button newButton = new Button(null);            newButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));            newButton.setId(a);            newButton.setText("Button " + a);            buttonPanel.addView(newButton);            a++;            startActivity(newForm);        }    });下面是xml代碼<HorizontalScrollView    android:id="@+id/buttonPanelChallengeMode"    android:layout_width="match_parent"    android:layout_height="180dp"    android:layout_below="@+id/IDE"    android:layout_marginBottom="3dp"    android:layout_marginLeft="8dp"    android:layout_marginRight="8dp"    android:background="@color/colorPrimary"    app:layout_constraintBottom_toBottomOf="parent"    tools:layout_editor_absoluteX="8dp">    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:orientation="vertical"        android:id="@+id/LinearButtonPanel">    </LinearLayout></HorizontalScrollView>
查看完整描述

3 回答

?
梵蒂岡之花

TA貢獻1900條經(jīng)驗 獲得超5個贊

這是因為在將按鈕添加到布局后,您將啟動新的 Activity,無論它是同一個 Activity。每當(dāng)創(chuàng)建活動時,它將始終使用初始 xml 格式。我認(rèn)為您的印象是添加新按鈕將持續(xù)存在并成為 XML 的一部分。如果你想開始新的活動,那不是真的。在 Bundle 中設(shè)置新的 Button 值,然后在 onCreate 檢查 bundle 的存在。如果存在,則添加一個新按鈕。


int buttonId = -1;

protected void onCreate(Bundle b){

     //set the layout related stuff first

     Bundle b = getIntent().getExtras();

     if(b!= null && (b.getInt(NEW_BUTTON_KEY, -1)!=-1)){

        buttonPanel = (LinearLayout)findViewById(R.id.LinearButtonPanel);

        for(int i = 0; i< b.getInt(NEW_BUTTON_KEY, -1); i++) 

        Button newButton = new Button(this);

        newButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        newButton.setId(i);

        newButton.setText("Button " + i);

        buttonPanel.addView(newButton);


     }

}

順便說一下,你為什么要進行新的活動?只需在同一個活動中添加按鈕,否則您的活動堆棧將隨著每個級別變得龐大。


查看完整回答
反對 回復(fù) 2021-06-30
?
呼啦一陣風(fēng)

TA貢獻1802條經(jīng)驗 獲得超6個贊

Button newButton = new Button(null);

你給上下文空,我建議你給適當(dāng)?shù)纳舷挛?。您也可以為按鈕設(shè)置標(biāo)簽newButton.setTag(value)


查看完整回答
反對 回復(fù) 2021-06-30
?
慕田峪9158850

TA貢獻1794條經(jīng)驗 獲得超7個贊

您的代碼不正確,您創(chuàng)建一個按鈕并添加到 LinearLayout,然后調(diào)用 startActivity 以加載 Activity。所以你重置了 LinearLayout 并清除了按鈕。


你應(yīng)該 :


run = (Button)findViewById(R.id.btnRunChallengeMode);


run.setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View view) {


        Intent newForm = new Intent(Form2.this,Form2.class);


        newForm.putExtra("a", a);


        startActivity(newForm);



    }

});

并在 create 中,獲取 Extras :


String a = getIntent().getIntExtra("a");

現(xiàn)在您可以創(chuàng)建按鈕了。


查看完整回答
反對 回復(fù) 2021-06-30
  • 3 回答
  • 0 關(guān)注
  • 235 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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