为了让自己的酷欧天气有个能够根据数据库动态添加TextView和按钮来显示已添加城市的界面,便开始着手于如标题所示的一个小Demo。
效果如图,能同时动态添加Button和TextView,还能通过动态添加的点击事件同时删除被点击的Button和TextView。

界面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.button_add2.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:isScrollContainer="true"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#34ab8b"
android:layout_margin="10dip"
android:text="添加"
android:textSize="18dp"
android:textColor="#fff"/>
<Button
android:id="@+id/btn_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#34ab8b"
android:layout_margin="10dip"
android:text="编辑"
android:textSize="18dp"
android:textColor="#fff"/>
</LinearLayout>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
private LinearLayout linearLayout;
//Button索引
private LinkedList<Button> ListBtn_Show;
//TextView索引
private LinkedList<TextView> ListText_Def;
private Button btn_add, btn_edit;
//判断btn_edit的状态
private int EDITSTATE = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inited();
}
private void inited() {
linearLayout = (LinearLayout) findViewById(R.id.linearlayout);
ListBtn_Show = new LinkedList<Button>();
ListText_Def = new LinkedList<TextView>();
btn_edit = (Button) findViewById(R.id.btn_edit);
btn_add = (Button) findViewById(R.id.btn_add);
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addBtn();//动态添加按钮
}
});
btn_edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//判断编辑按钮的状态
if (EDITSTATE == 0) {
btn_edit.setText("确定");
EDITSTATE = 1;
} else if (EDITSTATE == 1) {
btn_edit.setText("编辑");
EDITSTATE = 0;
}
}
});
}
private void addBtn() {//动态添加按钮
//添加承载两个按钮的LinearLayout
LinearLayout linear_btn = new LinearLayout(this);
linear_btn.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams liParam = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
linear_btn.setLayoutParams(liParam);
//添加Button
Button btnShow = new Button(this);
LinearLayout.LayoutParams btnAddPar = new LinearLayout.LayoutParams
(ViewGroup.LayoutParams.WRAP_CONTENT, 80, 3);//设置宽高及占比
btnAddPar.setMargins(0, 10, 0, 10);
btnShow.setLayoutParams(btnAddPar);
btnShow.setText(ListBtn_Show.size() + "");
btnShow.setTextColor(Color.argb(255, 255, 255, 255));
btnShow.setBackgroundColor(Color.argb(255, 52, 171, 139));
btnShow.setOnClickListener(new View.OnClickListener() {//动态添加点击事件
@Override
public void onClick(View view) {
if (EDITSTATE == 1)
delBtn(view);//动态删除按钮
}
});
linear_btn.addView(btnShow);//把btnShow添加到linear_btn中
ListBtn_Show.add(btnShow);//把btnShow添加到索引中
//添加TextView
TextView textDef = new TextView(this);
LinearLayout.LayoutParams btnDefPar = new LinearLayout.LayoutParams
(ViewGroup.LayoutParams.WRAP_CONTENT, 80, 1);//设置宽高及占比
btnDefPar.setMargins(0, 10, 0, 10);
textDef.setLayoutParams(btnDefPar);
textDef.setText("设为默认");
textDef.setGravity(Gravity.CENTER);
textDef.setTextColor(Color.argb(255, 255, 255, 255));
textDef.setBackgroundColor(Color.argb(255, 52, 171, 139));
textDef.setOnClickListener(new View.OnClickListener() {//动态添加点击事件
@Override
public void onClick(View view) {
setDef(view);//设置默认
}
});
linear_btn.addView(textDef);//把textDef添加到linear_btn中
ListText_Def.add(textDef);//把textDef添加到索引中
linearLayout.addView(linear_btn);//把linear_btn添加到外层linearLayout中
}
private void setDef(View view){//设置默认
//遍历索引里的所有TextView
for(int i=0;i<ListText_Def.size();i++){
ListText_Def.get(i).setBackgroundColor(Color.argb(255, 52, 171, 139));
ListText_Def.get(i).setText("设为默认");
if(ListText_Def.get(i)==view){
view.setBackgroundColor(Color.argb(255, 171, 52, 56));
ListText_Def.get(i).setText("默认");
}
}
}
private void delBtn(View view) {//动态删除按钮
if (view == null) {
return;
}
int position = -1;
for (int i = 0; i < ListBtn_Show.size(); i++) {
if (ListBtn_Show.get(i) == view) {
position = i;
break;
}
}
if (position >= 0) {
ListBtn_Show.remove(position);//从索引中移除被删除的Button
ListText_Def.remove(position);//从索引中移除被删除的TextView
linearLayout.removeViewAt(position + 1);//在外出linearLayout删除内部指定位置所有控件
}
}
}
网友评论