簡體   English   中英

如何知道從動態添加的視圖中單擊了哪些切換按鈕?

[英]How to know which Toggle Buttons are clicked from dynamically added view?

這是我第一次使用android編程,我被卡住了。

現在,我試圖動態添加包含切換按鈕和edittext的視圖。 但是,每當我選擇切換按鈕時,我創建的選項僅適用於上次創建的視圖。

選項很簡單。 有兩個切換按鈕,可以相互排斥地單擊它們

這意味着每當我在上面添加新的視圖(例如B和C)時,這些選項僅在C上有效,而在B中則無效。如何使它在每個視圖上都能使用?

public void onAddField(View v){
    LayoutInflater inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View rowView=inflater.inflate(R.layout.data_gledger_add_new,null);

    tbg_add=(ToggleButton)rowView.findViewById(R.id.add_toggle_gledger);
    tbc_add=(ToggleButton)rowView.findViewById(R.id.add_toggle_credit);

    if(create_box<4){
        csl.addView(rowView,csl.getChildCount()-1);
        Log.d("create_box",String.valueOf(create_box));
        create_box++;
    }
    else{
        Log.d("create_box","full");
        create_box=4;
    }

    tbg_add.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            if(tbg_add.isChecked()){
                get_add_cla="menu1";
                tbg_add.setTextColor(getResources().getColor(R.color.color_white));
                tbc_add.setChecked(false);
                tbc_add.setTextColor(getResources().getColor(R.color.color_black));
            }
            else{
                get_add_cla="";
                tbg_add.setTextColor(getResources().getColor(R.color.color_black));
            }
        }
    });

    //대변 선택
    tbc_add.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            if(tbc_add.isChecked()){
                get_add_cla="menu2";
                tbc_add.setTextColor(getResources().getColor(R.color.color_white));
                tbg_add.setChecked(false);
                tbg_add.setTextColor(getResources().getColor(R.color.color_black));
            }
            else{
                get_add_cla="";
                tbc_add.setTextColor(getResources().getColor(R.color.color_black));
            }
        }
    });
}

我忘了提到通過單擊按鈕添加視圖。 安卓的onClick = “onAddField”

問題幾乎可以肯定是由於您在動態添加新視圖時重復使用實例字段( tbg_addtbc_add )這一事實。

 tbg_add=(ToggleButton)rowView.findViewById(R.id.add_toggle_gledger); tbc_add=(ToggleButton)rowView.findViewById(R.id.add_toggle_credit); 

因為您要重新分配這些字段,並且還要從單擊偵聽器中引用它們,所以您將始終引用最新創建的切換按鈕。

將它們更改為局部變量,一切都應該正常工作。

ToggleButton ledger=(ToggleButton)rowView.findViewById(R.id.add_toggle_gledger);
ToggleButton credit=(ToggleButton)rowView.findViewById(R.id.add_toggle_credit);

與您的問題無關,但也應該解決的問題是,您將null作為第二個參數傳遞給inflate()調用:

 final View rowView=inflater.inflate(R.layout.data_gledger_add_new,null); 

當您以這種方式傳遞null時,系統將無法為新膨脹的視圖正確處理LayoutParams (xml文件中以android:layout_開頭的任何內容)。

您知道將要把rowView添加到csl視圖中,因此應該將其作為第二個參數傳遞。 完成此操作后,還必須將false作為第三個參數傳遞,以確保rowView inflate()調用實際上返回rowView而不是其新的父級( csl )。

final View rowView=inflater.inflate(R.layout.data_gledger_add_new, csl, false);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM