click event for all dynamic generated buttons android

你说的曾经没有我的故事 提交于 2019-12-04 05:30:58

问题


I write code to generate dynamic buttons , but I don't know how to implement click event for each button dynamically. I found some answers but not work with my code... Please help me.. This is my code

public class dynamicbuttion extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        ScrollView sv = new ScrollView(this);
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        sv.addView(ll);

        for(int i = 0; i < 5; i++) {
            Button btn = new Button(this);
        btn.setId(i);
        btn.setText("dynamic buttion " + i);
            ll.addView(btn);

        }

        this.setContentView(sv);

    }
}

回答1:


Button[] btn = new Button[5];
for(int i = 0; i < 5; i++) {
    btn[i] = new Button(this);
    btn[i].setId(i);
    btn[i].setText("dynamic buttion " + i);
    ll.addView(btn[i]);
    btn[i].setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //your desired functionality
    }
  });
}



回答2:


I have done by using Custom Layout:

private LinearLayout linearLayoutParent;

onCreate:

linearLayoutParent = (LinearLayout)findViewById(R.id.linearLayoutParent);

Now whenever need to create dynamic textviews, simply we will add to Parent Linear Layout:

TextView[] name = new TextView[10];
for (int i = 0; i < 10; i++) {
    View view = getLayoutInflater().inflate(R.layout.child_view, linearLayoutParent, false);
    name[i] = (TextView) view.findViewById(R.id.child_name);
    name[i].setText("Dynamic Textxview " + i);
    name[i].setId(i);
    name[i].setTag(String.valueOf(i));
    ll.addView(view);
    name[i].setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.i("Clicked", ""+v.getTag());
        }
    });
}

child_view.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/child_name"
    android:layout_width="120dp"
    android:layout_height="50dp"
    android:background="#969696"
    android:gravity="center"
    android:text=""
    android:textColor="#fff"
    android:textSize="16sp" />

Hope this will help you.




回答3:


Use an anonymous OnClickListener implementation. Like this:

btn.setOnClickListener(new OnClickListener()
{
    public void onClick(View v)
    {
        doThisWhenClicked();
    }
});



回答4:


public class DynamicButton extends Activity {

    private LinearLayout ll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dynic_button);

        ll = (LinearLayout) findViewById(R.id.llContent);
        Button[] dynamic_button = new Button[10];
        for (int i = 0; i < 10; i++) {
            dynamic_button[i] = new Button(this);
            dynamic_button[i].setId(i);
            dynamic_button[i].setTag("" + i);
            dynamic_button[i].setText("My Dynamic Button No: " + i);
            ll.addView(dynamic_button[i]);
            dynamic_button[i].setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Log.i("Clicked", "" + v.getTag());
                }
            });
        }

    }
}



回答5:


For dynamic requirements list will be the best option,Also you can inflate XML view for dynamic drawing

try {
        int brotherCount =5;
        List<View> dynamicView = new ArrayList<>();
        List<TextInputEditText> dynamicBrotherText = new ArrayList<>();
        List<TextInputLayout> dynamicLayoutBrother = new ArrayList<>();
        for(int i=1;i<=brotherCount;i++){
            View family_static = getLayoutInflater()
                    .inflate(R.layout.my_family_static, mLayoutBrother, false);
            TextInputEditText  inputEditText=family_static.findViewById(R.id.TextInputEditTextFather);
            TextInputLayout inputLayout=family_static.findViewById(R.id.TextInputLayoutFamily);
            inputEditText.setText("name"+i);
            inputLayout.setHint(getResources().getString(R.string.about_my_brother));
            dynamicBrotherText.add(inputEditText);
            dynamicLayoutBrother.add(inputLayout);
            dynamicView.add(family_static);
            mLayoutBrother.addView(family_static);
        }
        for(View  view :dynamicBrotherText){
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showToast(((TextInputEditText)view).getText().toString());
                }
            });
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.d("", "createBrotherLayout: ");
    }


来源:https://stackoverflow.com/questions/19823419/click-event-for-all-dynamic-generated-buttons-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!