[英]delete a dynamically created button android
好的,我想做的是,类似于标签搜索功能。 我所做的是创建 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 错误。 谢谢
由于声誉,我无法发表评论。 所以我在这里发帖。
在按钮单击侦听器中:
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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.