简体   繁体   English

删除一个动态创建的按钮android

[英]delete a dynamically created button android

Ok, what I'm trying to do is, something like a tag search function.好的,我想做的是,类似于标签搜索功能。 What I did was create Button ArrayList, and whenever a user tries to input ",", it automatically creates a button and inserts it into the ArrayList, which ultimately registers to the LinearLayout.我所做的是创建 Button ArrayList,每当用户尝试输入“,”时,它会自动创建一个按钮并将其插入到 ArrayList 中,后者最终注册到 LinearLayout。

It creates well, but does not delete well.它创建得很好,但删除得不好。 I get the error that the index is out of bounds, but was in no luck of finding out which statement should I use.我收到索引超出范围的错误,但我没有找到应该使用哪个语句。 Could you help me?你可以帮帮我吗? Thx in advance.提前谢谢。

public class MainActivity extends AppCompatActivity {

private ArrayList<Button> tagCollection = null;
private LinearLayout llTagCollection = null;

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

    tagCollection = new ArrayList<Button>();
    initView();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void initView() {
    final EditText etSearch = (EditText) findViewById(R.id.ET_SEARCH);
    // final TextView tvResult = (TextView) findViewById(R.id.TV_RESULT);
    llTagCollection = (LinearLayout) findViewById(R.id.LL_BUTTON_COLLECTION);


    View currentView = new View(getApplicationContext());
    /*
    if(tagCollection.size() != 0) {
        tagCollection.get(currentView.getId()).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tagCollection.remove(tagCollection.get(v.getId()));
                Log.e("Removed", String.valueOf(tagCollection.remove(tagCollection.get(v.getId()))));
                // tagCollection.remove(this);
                llTagCollection.removeAllViews();
                int i = 0;
                while (i < (tagCollection.size())) {
                    llTagCollection.addView(tagCollection.get(i));
                    i += 1;
                }
            }
        });
    }
    */

    TextWatcher tagWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            String convertedCharSequence = s.toString();
            Log.e("ConvertedString", convertedCharSequence);
            if (convertedCharSequence.length() > 0) {
                String subString = convertedCharSequence.substring(convertedCharSequence.length() - 1);
                Log.e("SubStringPosition", subString);

                if (subString.equals(",")) {
                    // tvResult.setText(etSearch.getText().toString());
                    Button bTagButton = new Button(MainActivity.this);
                    bTagButton.setId(tagCollection.size());
                    bTagButton.setText(etSearch.getText().toString());
                    bTagButton.setBackgroundColor(Color.WHITE);
                    bTagButton.setTextColor(Color.BLACK);
                    /*
                    RelativeLayout.LayoutParams rlLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

                    if(tagCollection.size() != 0){
                        rlLayoutParams.addRule(RelativeLayout.END_OF, tagCollection.get(0).getId());
                    }
                    bTagButton.setLayoutParams(rlLayoutParams);
                    */

                    bTagButton.setOnClickListener(new View.OnClickListener(){
                        @Override
                        public void onClick(View v) {
                            tagCollection.remove(tagCollection.indexOf(this));

                            llTagCollection.removeAllViews();
                            int i = 0;
                            while (i < (tagCollection.size())) {
                                if(tagCollection.get(i) != null){
                                    llTagCollection.addView(tagCollection.get(i));
                                }

                                i += 1;
                            }
                        }
                    });


                    tagCollection.add(bTagButton);

                    llTagCollection.removeAllViews();
                    int i = 0;
                    while (i < (tagCollection.size())) {
                        llTagCollection.addView(tagCollection.get(i));
                        i += 1;
                    }
                    // setContentView(rlTagCollection);
                    etSearch.setText("");
                    etSearch.setSelection(0);
                }

            }

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };
    etSearch.addTextChangedListener(tagWatcher);
}

} }

I solved it using indexOfChild function.我使用 indexOfChild 函数解决了它。 It kept on throwing me indexOutOfBounds error.它不断向我抛出 indexOutOfBounds 错误。 Thx谢谢

Because of the reputations, I can not add comment.由于声誉,我无法发表评论。 SO I post here.所以我在这里发帖。

In the button click listener:在按钮单击侦听器中:

bTagButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            tagCollection.remove(tagCollection.indexOf(this));

                            llTagCollection.removeAllViews();
                            int i = 0;
                            while (i < (tagCollection.size())) {
                                if (tagCollection.get(i) != null) {
                                    llTagCollection.addView(tagCollection.get(i));
                                }

                                i += 1;
                            }
                        }
                    });

You remove the button by tagCollection.remove(tagCollection.indexOf(this));您通过tagCollection.remove(tagCollection.indexOf(this));删除按钮. . Here this means the OnClickListener instance, not the button clicked. this意味着OnClickListener实例,而不是单击的按钮。 SO you should remove buttons by:所以你应该通过以下方式删除按钮:

tagCollection.remove(tagCollection.indexOf(v));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在android中编辑动态创建的按钮 - Edit dynamically created button in android 保存动态创建的按钮android - Saving dynamically created button android 侦听Android中动态创建的切换按钮 - Listening to dynamically created toggle button in android Android单击按钮即可删除动态创建的微调器和按钮 - Android remove dynamically created spinner and button with click of a button 如何获取在android中动态创建的单选按钮的id - How to get checked radio button's id created dynamically in android 将文本从动态创建的edittext放入按钮android java - getting text from dynamically created edittext into a button android java 通过动态创建按钮删除动态创建的ViewGroup - Removing a dynamically created ViewGroup by dynamically created button 动态生成的删除按钮不响应OnclickListener(Android JAVA) - Dynamically generated delete button does not respond to an OnclickListener(Android JAVA) 在动态创建的卡片视图上,如果按下特定于卡片的删除按钮,如何删除单个卡片? - On a card view created dynamically, how can I remove individual cards if a delete button specific to a card is pressed? 如何将删除按钮 (x) 附加到动态创建的 ListView 的每个成员? - How can I attach a delete button (x) to each member of a dynamically created ListView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM