簡體   English   中英

將不同的ID分配給同一視圖

[英]Assign different ids to the same view

一種父級線性布局會被放大,並且子視圖會添加到for循環中。 但是所有子視圖都具有相同的ID。 我可以通過v.getId()找到ID,然后將ID硬編碼到case "id of child view"case "id of child view" 但是ID可能會在另一部手機上更改。
在我的情況下,所有ID都相同,因此手動設置它們也可能是不好的做法。 我想通過onClick(View v) -Method調用子視圖,但是無法正確獲取ID。
我添加了一個樹視圖作為插圖: 樹視圖布局

@Override
    public void onCreate(Bundle savedInstanceState) {
        // extending existing parent class' methods
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout parent = (LinearLayout) inflater.inflate(R.layout.mainbase, null);
        for (int i = 0; i < 4; i++) {
            View custom = inflater.inflate(R.layout.identifierplate, null);
            switch (i) {
                case 0: {
                    //set layout images and text for first <include>
                    ImageView SensorReadout = (ImageView) custom.findViewById(R.id.sensor_icon);
                    SensorReadout.setImageResource(R.drawable.ic_launcher);
                    TextView sensor_name = (TextView) custom.findViewById(R.id.sensor_name);
                    sensor_name.setText(R.string.sr_1);
                    TextView sensor_description = (TextView) custom.findViewById(R.id.sensor_description);
                    sensor_description.setText(R.string.sr_2);
                    custom.setOnClickListener(this);
                    parent.addView(custom);
                    break;
                }
                case 1: {
                    // do same as in case 0 but different text/image
                    custom.setOnClickListener(this);
                    parent.addView(custom);
                    break;
                }      
                default: { // Defaults are already set in the XML                    
                }
            }
        }
        setContentView(parent);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case 0: {
                Intent intent = new Intent(this, First.class);
                startActivity(intent);
                break;
            }
            case 1: {
                Intent intent = new Intent(this, Second.class);
                startActivity(intent);
                break;
            }
            default: {
                int id = v.getId();
                String ids = String.valueOf(id);
                Toast.makeText(this, ids, Toast.LENGTH_SHORT).show(); //error: all have same id
            }
        }
    }

如何使用childview的setTag和getTag方法?

然后,您可以檢查getTag ==第一個孩子,並根據您的邏輯進行編碼。

在for循環中,您可以使用帶有i變量的setTag來設置標簽。

Mike M.向我指出了添加到.java的正確方向:

custom.setId(R.id.id_v_2);

添加到ids.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item
        type="id"
        name="id_v_0" />
    <item
        type="id"
        name="id_v_1" />
</resources>

更改了onClick()

public void onClick(View v) {
    switch (v.getId()) {
        case R.id.id_v_0: { ...

暫無
暫無

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

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