Android 動態(tài)添加view或item并獲取數(shù)據(jù)的實例
最近在做一項目,項目中用到了一個功能,要求是動態(tài)Item,而且是多個的情況下,不過仔細的分析了下,都大同小異,做起來也很簡單,在這里我只抽取出來做了一demo,也只做了一個動態(tài)添加item,同時可以獲取所有添加和編輯Item上的數(shù)據(jù),先上圖:

我們先來分析一下這個demo:
兩個TextView和EditText,一個Button,一個星級評價RatingBar控件,布局完事…
activity_dynamic的布局,有可能會添加多個,所以外面用ScrollView,因為我們是垂直方向添加,所以使用LinearLayout做容器
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:id="@+id/ll_addView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
<Button
android:id="@+id/btn_getData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ll_addView"
android:layout_marginTop="10dp"
android:background="@drawable/em_btn_green_selector"
android:text="獲取數(shù)據(jù)" />
</RelativeLayout>
</ScrollView>
再看看要添加的item_hotel_evaluate里面的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_hotelName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/editbox_background_normal">
<LinearLayout
android:id="@+id/rl_addHotel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_hotelName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:text="酒店名稱:"
android:textSize="18sp" />
<EditText
android:id="@+id/ed_hotelName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="@drawable/editbox_background_normal"
android:padding="5dp"
android:singleLine="true" />
<Button
android:id="@+id/btn_addHotel"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:background="@drawable/em_btn_green_selector"
android:text="+新增"
android:textColor="@color/white"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_addHotelEvaluate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rl_addHotel"
android:layout_marginTop="5dp"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/rl_hotelEvaluate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rl_addHotel"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_hotelServer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:text="服務評價:"
android:textSize="18sp" />
<RatingBar
android:id="@+id/rb_hotel_evaluate"
style="@style/myRatingBar"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_toRightOf="@+id/tv_hotelServer"
android:numStars="5"
android:rating="0"
android:stepSize="1.0" />
</RelativeLayout>
<EditText
android:id="@+id/ed_hotelEvaluate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rl_server"
android:background="@drawable/editbox_background_normal"
android:singleLine="true" />
</LinearLayout>
</RelativeLayout>
布局好了,因為Activity里面的代碼寫不是很多,直接上代碼了,然后在最后分析一下:
package com.bob.lucking.activity;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import com.bob.lucking.R;
/**
* Created by bob on 2017/3/20.
*/
public class DynamicAddViewActivity extends Activity implements View.OnClickListener {
private String TAG = this.getClass().getSimpleName();
//裝在所有動態(tài)添加的Item的LinearLayout容器
private LinearLayout addHotelNameView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dynamic);
addHotelNameView = (LinearLayout) findViewById(R.id.ll_addView);
findViewById(R.id.btn_getData).setOnClickListener(this);
//默認添加一個Item
addViewItem(null);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_addHotel://點擊添加按鈕就動態(tài)添加Item
addViewItem(v);
break;
case R.id.btn_getData://打印數(shù)據(jù)
printData();
break;
}
}
/**
* Item排序
*/
private void sortHotelViewItem() {
//獲取LinearLayout里面所有的view
for (int i = 0; i < addHotelNameView.getChildCount(); i++) {
final View childAt = addHotelNameView.getChildAt(i);
final Button btn_remove = (Button) childAt.findViewById(R.id.btn_addHotel);
btn_remove.setText("刪除");
btn_remove.setTag("remove");//設置刪除標記
btn_remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//從LinearLayout容器中刪除當前點擊到的ViewItem
addHotelNameView.removeView(childAt);
}
});
//如果是最后一個ViewItem,就設置為添加
if (i == (addHotelNameView.getChildCount() - 1)) {
Button btn_add = (Button) childAt.findViewById(R.id.btn_addHotel);
btn_add.setText("+新增");
btn_add.setTag("add");
btn_add.setOnClickListener(this);
}
}
}
//添加ViewItem
private void addViewItem(View view) {
if (addHotelNameView.getChildCount() == 0) {//如果一個都沒有,就添加一個
View hotelEvaluateView = View.inflate(this, R.layout.item_hotel_evaluate, null);
Button btn_add = (Button) hotelEvaluateView.findViewById(R.id.btn_addHotel);
btn_add.setText("+新增");
btn_add.setTag("add");
btn_add.setOnClickListener(this);
addHotelNameView.addView(hotelEvaluateView);
//sortHotelViewItem();
} else if (((String) view.getTag()).equals("add")) {//如果有一個以上的Item,點擊為添加的Item則添加
View hotelEvaluateView = View.inflate(this, R.layout.item_hotel_evaluate, null);
addHotelNameView.addView(hotelEvaluateView);
sortHotelViewItem();
}
//else {
// sortHotelViewItem();
//}
}
//獲取所有動態(tài)添加的Item,找到控件的id,獲取數(shù)據(jù)
private void printData() {
for (int i = 0; i < addHotelNameView.getChildCount(); i++) {
View childAt = addHotelNameView.getChildAt(i);
EditText hotelName = (EditText) childAt.findViewById(R.id.ed_hotelName);
RatingBar hotelEvaluateStart = (RatingBar) childAt.findViewById(R.id.rb_hotel_evaluate);
EditText hotelEvaluate = (EditText) childAt.findViewById(R.id.ed_hotelEvaluate);
Log.e(TAG, "酒店名稱:" + hotelName.getText().toString() + "-----評價星數(shù):"
+ (int) hotelEvaluateStart.getRating() + "-----服務評價:" + hotelEvaluate.getText().toString());
}
}
}
最后我們來解讀一下代碼:
onCreate里面初始化控件并設置事件,同時我們默認添加一條item,因為addHotelNameView容器初始化時里面沒有子view,所以我們默認給添加的方法傳null,
在addViewItem方法時,里面有初始化并設置button方法,所以在onclick方法里面把事件的v傳入是為了做標記,也就是設置tag,,在添加時會有兩種情況:
1.如果只有一條,我們只能顯示添加
2.有多條的情況下,如果點擊的是設置有tag為add標記的添加,則添加
如果點擊刪除,在sortHotelViewItem方法里面已經設置過刪除點擊事件,直接從內存中刪除,
最后是獲取數(shù)據(jù),我們可以通過LinearLayout容器來遍歷addHotelNameView.getChildCount()獲取所有添加的item,然后找到控件的id去獲取所有添加的item數(shù)據(jù)。
再這里注釋一下:在addViewItem方法里面看到可以優(yōu)化,上傳資源時已經打包好了,現(xiàn)在在這里用單行注釋掉了4行,添加第一個item時不需要排序的,還有就是else里面的是死代碼,下載資源的朋友些可以刪除這幾行。
以上這篇Android 動態(tài)添加view或item并獲取數(shù)據(jù)的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Android短信驗證碼監(jiān)聽解決onChange多次調用的方法
本篇文章主要介紹了Android短信驗證碼監(jiān)聽解決onChange多次調用的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
關于如何使用Flutter開發(fā)執(zhí)行操作系統(tǒng)shell命令的工具詳解
本文主要介紹如何在Flutter應用中開發(fā)一個Android終端命令行工具,包括終端命令行頁面的布局設計、與Shell通信的基本原理、輸入輸出處理的基本技巧等,以及如何在具體應用中利用終端命令行工具來執(zhí)行系統(tǒng)命令和與用戶進行交互2023-06-06
Android ViewPager循環(huán)播放廣告實例詳解
這篇文章主要介紹了Android ViewPager循環(huán)播放廣告條實例詳解的相關資料,需要的朋友可以參考下2017-03-03
Flutter定時器、倒計時的快速上手及實戰(zhàn)講解
這篇文章主要給大家介紹了關于Flutter定時器、倒計時的快速上手及實戰(zhàn)的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Flutter具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-06-06

