爲了讓自己的酷歐天氣有個能夠根據數據庫動態添加TextView和按鈕來顯示已添加城市的界面,便開始着手於如標題所示的一個小Demo。
效果如圖,能同時動態添加Button和TextView,還能通過動態添加的點擊事件同時刪除被點擊的Button和TextView。
add.gif
<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刪除內部指定位置所有控件
}
}
}