Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当动态多次膨胀布局xml时,如何区分或识别按钮小部件?_Android - Fatal编程技术网

Android 当动态多次膨胀布局xml时,如何区分或识别按钮小部件?

Android 当动态多次膨胀布局xml时,如何区分或识别按钮小部件?,android,Android,我多次膨胀一个有按钮的xml,我能够完美地这样做,但问题是当我单击按钮时,我想显示单击了哪个按钮 public class InflateExActivity extends Activity implements OnClickListener { /** Called when the activity is first created. */ Button b; LinearLayout lLayout; LayoutInflater infla

我多次膨胀一个有按钮的xml,我能够完美地这样做,但问题是当我单击按钮时,我想显示单击了哪个按钮

   public class InflateExActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */

    Button b;


    LinearLayout lLayout;
    LayoutInflater inflater;

    @Override
    public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        for (int i = 0; i < 3; i++) {

             inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             b = (Button) inflater.inflate(R.layout.buttons, null);
            t = (TextView) inflater.inflate(R.layout.texts, null);


            b.setTag(i); // you'll get 0,1,2 as

            lLayout = (LinearLayout) findViewById(R.id.layout1);
            lLayout.addView(b);

            b.setOnClickListener(this);

        }

    }

    public void onClick(View v) {

        }

}
public类InflateExActivity扩展活动实现OnClickListener{
/**在首次创建活动时调用*/
按钮b;
线性布局;
充气机;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
对于(int i=0;i<3;i++){
充气器=(LayoutInflater)getSystemService(Context.LAYOUT\u充气器\u SERVICE);
b=(按钮)充气器。充气(R.layout.buttons,null);
t=(TextView)充气器。充气(R.layout.text,null);
b、 setTag(i);//您将得到0,1,2作为
lLayout=(线性布局)findViewById(R.id.layout1);
lLayout.addView(b);
b、 setOnClickListener(此);
}
}
公共void onClick(视图v){
}
}

以编程方式添加的项目,必须为其分配ID

b.setId(1);
编辑:

public class DynamicLayoutActivity extends Activity implements OnClickListener{
private static final int MY_BUTTON = 9000;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LinearLayout ll = (LinearLayout)findViewById(R.id.layout1);

    // add button
    Button b = new Button(this);
    b.setText("Button added dynamically!");
    b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,   LayoutParams.WRAP_CONTENT));
    b.setId(MY_BUTTON);
    b.setOnClickListener(this);
    ll.addView(b);
}
 public void onClick(View v) {
        Toast toast;
        Log.w("ANDROID DYNAMIC VIEWS:", "View Id: " + v.getId());
        switch (v.getId()) {
        case MY_BUTTON:
            toast = Toast.makeText(this, "Clicked on my dynamically added button!", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.TOP, 25, 400);
            toast.show();    
        }
    }
最新:

public class InflateExActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        LinearLayout lLayout;
        Button b = null;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        for(int i=0;i<3;i++){
            final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             b = (Button) inflater.inflate(R.layout.buttons, null);
             b.setId(i);
            lLayout = (LinearLayout) findViewById(R.id.layout1);
            lLayout.addView(b);
            b.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Toast.makeText(InflateExActivity.this, "Button Clicked :"+v.getId(),
                        Toast.LENGTH_LONG).show();
            }
        });
    }
}  
public class InflateExActivity扩展了活动{
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
线性布局;
按钮b=空;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
对于(int i=0;i使用视图标记

View.setTag(对象标记)

您可以将字符串或复杂对象(如类)设置为标记。

您可以对每个按钮使用setTag()。在for循环中,您可以指定button.setTag()。您还可以使用getTag()检索按钮的标记。展开布局后,向按钮添加标记

编辑2: 您应该为布局充气,然后查找您的按钮id。请参见以下内容:

    public class InflateExActivity extends Activity {
            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {
                LinearLayout lLayout;
                final Button b = null;
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                for(int i=0;i<3;i++){
                    final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.buttons, null);
b = v.findViewById(R.id.your_button_id);
    //                 b = (Button) inflater.inflate(R.layout.buttons, null);
                       b.setTag(i); // you'll get 0,1,2 as tags
                    lLayout = (LinearLayout) findViewById(R.id.layout1);
                    lLayout.addView(b);
                    b.setOnClickListener(new OnClickListener() {

                        public void onClick(View v) {
    int specificButton = (Integer)v.getTag();//Changed here.......
                            Toast.makeText(InflateExActivity.this, "Button Clicked"+Integer.toString(specificButton),
                                    Toast.LENGTH_LONG).show();
                        }
                    });
                }
            }  
        }
public class InflateExActivity扩展了活动{
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
线性布局;
最终按钮b=null;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

对于(int i=0;i表示这不是我要找的嘿这不是我要找的..在三个按钮中,我想知道单击了哪一个..嘿,这也起作用..我们犯了一个错误..在onClick中,而不是b.getTag()…应该是v.getTag();我编辑了它..请接受..这样我可以将它标记为upvote以表明这也是正确的方法..您知道变量名可以而且应该不止一个字母长。