当动态充气布局XML多次,我怎么能区分或识别按钮控件?控件、布局、按钮、动态

2023-09-05 01:52:38 作者:二狗子

我膨胀了具有XML按钮,多次,我能够做的如此完美,但问题是,当我按一下按钮,我想说明单击该按钮。

 公共类InflateExActivity扩展活动实现OnClickListener {
    / **第一次创建活动时调用。 * /

    按钮B;


    的LinearLayout lLayout;
    LayoutInflater充气;

    @覆盖
    公共无效的onCreate(包savedInstanceState){


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

             充气=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             B =(按钮)inflater.inflate(R.layout.buttons,NULL);
            T =(TextView中)inflater.inflate(R.layout.texts,NULL);


            b.setTag(ⅰ); //你会得到0,1,2为

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

            b.setOnClickListener(本);

        }

    }

    公共无效的onClick(视图v){

        }

}
 

解决方案

项目要添加编程,您必须有到IDS分配给他们。

  b.setId(1);
 

编辑:

 公共类DynamicLayoutActivity扩展活动实现OnClickListener {
私有静态最终诠释MY_BUTTON = 9000;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    的LinearLayout LL =(的LinearLayout)findViewById(R.id.layout1);

    //添加按钮
    按钮B =新的按钮(这一点);
    b.setText(按钮,动态地添加!);
    b.setLayoutParams(新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
    b.setId(MY_BUTTON);
    b.setOnClickListener(本);
    ll.addView(B);
}
 公共无效的onClick(视图v){
        吐司面包;
        Log.w(机器人动态视图,查看编号:+ v.getId());
        开关(v.getId()){
        案例MY_BUTTON:
            敬酒= Toast.makeText(这一点,点击我的动态添加的按钮!,Toast.LENGTH_LONG);
            toast.setGravity(Gravity.TOP,25,400);
            toast.show();
        }
    }
 
QT从入门到实战x篇 xx 登录窗口布局 手动布局 widget 行列形式的用栅格 弹簧调整控件与widget距离

最新消息:

 公共类InflateExActivity延伸活动{
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        的LinearLayout lLayout;
        按钮B = NULL;
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        的for(int i = 0;我3;;我++){
            最后LayoutInflater充气=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             B =(按钮)inflater.inflate(R.layout.buttons,NULL);
             b.setId(ⅰ);
            lLayout =(的LinearLayout)findViewById(R.id.layout1);
            lLayout.addView(B);
            b.setOnClickListener(新OnClickListener(){

            公共无效的onClick(视图v){
                Toast.makeText(InflateExActivity.this,单击按钮+ v.getId()
                        Toast.LENGTH_LONG).show();
            }
        });
    }
}
 

I am inflating an xml having button, multiple times and i am able to do so perfectly but the problem is when I click the button,i want to show which button is clicked.

   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) {

        }

}

解决方案

items you are adding programmatically, you must have to assign ids to them.

b.setId(1);

EDITED:

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();    
        }
    }

LATEST:

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();
            }
        });
    }
}