国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 動態添加view或item并獲取數據的實例

2019-10-22 18:26:40
字體:
來源:轉載
供稿:網友

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

Android,動態添加,view,item

我們先來分析一下這個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="獲取數據" />  </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();  //裝在所有動態添加的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://點擊添加按鈕就動態添加Item        addViewItem(v);        break;      case R.id.btn_getData://打印數據        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();    //}  }  //獲取所有動態添加的Item,找到控件的id,獲取數據  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() + "-----評價星數:"          + (int) hotelEvaluateStart.getRating() + "-----服務評價:" + hotelEvaluate.getText().toString());    }  }}

最后我們來解讀一下代碼:

onCreate里面初始化控件并設置事件,同時我們默認添加一條item,因為addHotelNameView容器初始化時里面沒有子view,所以我們默認給添加的方法傳null,

在addViewItem方法時,里面有初始化并設置button方法,所以在onclick方法里面把事件的v傳入是為了做標記,也就是設置tag,,在添加時會有兩種情況:

1.如果只有一條,我們只能顯示添加

2.有多條的情況下,如果點擊的是設置有tag為add標記的添加,則添加

如果點擊刪除,在sortHotelViewItem方法里面已經設置過刪除點擊事件,直接從內存中刪除,

最后是獲取數據,我們可以通過LinearLayout容器來遍歷addHotelNameView.getChildCount()獲取所有添加的item,然后找到控件的id去獲取所有添加的item數據。

再這里注釋一下:在addViewItem方法里面看到可以優化,上傳資源時已經打包好了,現在在這里用單行注釋掉了4行,添加第一個item時不需要排序的,還有就是else里面的是死代碼,下載資源的朋友些可以刪除這幾行。

以上這篇Android 動態添加view或item并獲取數據的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 葵青区| 东丰县| 亳州市| 浙江省| 广饶县| 德清县| 德兴市| 迁西县| 东乌| 广元市| 内丘县| 定襄县| 城固县| 论坛| 棋牌| 修武县| 涟源市| 应城市| 肇庆市| 许昌县| 南陵县| 阿拉尔市| 竹北市| 江北区| 科尔| 旅游| 海原县| 清徐县| 福建省| 犍为县| 小金县| 北碚区| 商城县| 惠州市| 长沙市| 福鼎市| 仲巴县| 枞阳县| 始兴县| 建平县| 清远市|