Java 删除动态创建的按钮
好的,我想做的是,类似于标签搜索功能。 我所做的是创建按钮ArrayList,每当用户尝试输入“,”时,它会自动创建一个按钮并将其插入ArrayList,ArrayList最终注册到LinearLayout 它可以创建井,但不能删除井。 我得到的错误是索引超出了范围,但没有找到应该使用哪个语句。你能帮我吗? Thx提前Java 删除动态创建的按钮,java,android,button,android-linearlayout,Java,Android,Button,Android Linearlayout,好的,我想做的是,类似于标签搜索功能。 我所做的是创建按钮ArrayList,每当用户尝试输入“,”时,它会自动创建一个按钮并将其插入ArrayList,ArrayList最终注册到LinearLayout 它可以创建井,但不能删除井。 我得到的错误是索引超出了范围,但没有找到应该使用哪个语句。你能帮我吗? Thx提前 public class MainActivity extends AppCompatActivity { private ArrayList<Button> ta
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);
}
public类MainActivity扩展了AppCompatActivity{
私有ArrayList tagCollection=null;
private LinearLayout llTagCollection=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tagCollection=newarraylist();
initView();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
公共视图(){
最终EditText etSearch=(EditText)findViewById(R.id.etu SEARCH);
//最终文本视图tvResult=(文本视图)findViewById(R.id.TV_结果);
llTagCollection=(LinearLayout)findViewById(R.id.LL\u按钮\u集合);
View currentView=新视图(getApplicationContext());
/*
if(tagCollection.size()!=0){
tagCollection.get(currentView.getId()).setOnClickListener(new View.OnClickListener()){
@凌驾
公共void onClick(视图v){
tagCollection.remove(tagCollection.get(v.getId());
Log.e(“已删除”,String.valueOf(tagCollection.remove(tagCollection.get(v.getId())));
//tagCollection。移除(此);
llTagCollection.removeallview();
int i=0;
而(i<(tagCollection.size()){
llTagCollection.addView(tagCollection.get(i));
i+=1;
}
}
});
}
*/
TextWatcher tagWatcher=新的TextWatcher(){
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after){
}
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
字符串convertedCharSequence=s.toString();
Log.e(“ConvertedString”,convertedCharSequence);
if(convertedCharSequence.length()>0){
String subString=convertedCharSequence.subString(convertedCharSequence.length()-1);
Log.e(“子字符串位置”,子字符串);
if(子字符串等于(“,”){
//tvResult.setText(etSearch.getText().toString());
按钮bTagButton=新按钮(MainActivity.this);
bTagButton.setId(tagCollection.size());
bTagButton.setText(etSearch.getText().toString());
bTagButton.setBackgroundColor(颜色:白色);
bTagButton.setTextColor(Color.BLACK);
/*
RelativeLayout.LayoutParams rlLayoutParams=新的RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_内容,RelativeLayout.LayoutParams.WRAP_内容);
if(tagCollection.size()!=0){
rlLayoutParams.addRule(RelativeLayout.END_OF,tagCollection.get(0.getId());
}
bTagButton.setLayoutParams(rlLayoutParams);
*/
bTagButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
tagCollection.remove(tagCollection.indexOf(this));
llTagCollection.removeallview();
int i=0;
而(i<(tagCollection.size()){
if(tagCollection.get(i)!=null){
llTagCollection.addView(tagCollection.get(i));
}
i+=1;
}
}
});
tagCollection.add(bTagButton);
llTagCollection.removeallview();
int i=0;
而(i<(tagCollection.size()){
llTagCollection.addView(tagCollection.get(i));
i+=1;
}
//setContentView(rlTagCollection);
etSearch.setText(“”);
etSearch.setSelection(0);
}
}
}
@凌驾
公共无效后文本已更改(可编辑){
}
};
etSearch.addTextChangedListener(标记观察者);
}
}我用indexOfChild函数解决了这个问题。它不断地向我抛出索引自动边界错误。由于声誉问题,我无法发表评论。所以我在这里发帖 在按钮中,单击侦听器:
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;
}
}
});
链接记录器输出我使用indexOfChild函数解决了它。它不断地向我抛出索引自动边界错误。Thx@DevMGL当前位置您应该将此作为答案发布并接受它,以便其他人可以看到您的问题已经解决,尤其是因为您自己解决了问题。
tagCollection.remove(tagCollection.indexOf(v));