【问题标题】:delete a dynamically created button android删除一个动态创建的按钮android
【发布时间】:2015-08-28 01:35:23
【问题描述】:

好的,我想做的是,类似于标签搜索功能。 我所做的是创建Button ArrayList,每当用户尝试输入“,”时,它会自动创建一个按钮并将其插入ArrayList,最终注册到LinearLayout。

它可以很好地创建,但不能很好地删除。 我得到索引超出范围的错误,但没有找到我应该使用哪个语句。你可以帮帮我吗? 提前谢谢。

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

}

【问题讨论】:

  • 链接记录器输出
  • 我使用 indexOfChild 函数解决了它。它不断向我抛出 indexOutOfBounds 错误。谢谢
  • @DevMGL:您应该将此作为答案发布并接受它,以便其他人可以看到您的问题已解决,尤其是因为您自己解决了问题。

标签: java android button android-linearlayout


【解决方案1】:

我使用 indexOfChild 函数解决了这个问题。它不断向我抛出 indexOutOfBounds 错误。谢谢

【讨论】:

    【解决方案2】:

    由于声誉,我无法添加评论。所以我在这里发帖。

    在按钮点击监听器中:

    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.remove(tagCollection.indexOf(this)); 的按钮。这里this 表示OnClickListener 实例,而不是单击的按钮。所以你应该删除按钮:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-02
      • 2019-12-21
      • 2012-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多