欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 動態(tài)添加view或item并獲取數(shù)據(jù)的實例

 更新時間:2017年10月09日 11:02:31   作者:波興楊  
下面小編就為大家?guī)硪黄狝ndroid 動態(tài)添加view或item并獲取數(shù)據(jù)的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

最近在做一項目,項目中用到了一個功能,要求是動態(tài)Item,而且是多個的情況下,不過仔細(xì)的分析了下,都大同小異,做起來也很簡單,在這里我只抽取出來做了一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="服務(wù)評價:"
        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);

    //默認(rèn)添加一個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");//設(shè)置刪除標(biāo)記
      btn_remove.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        //從LinearLayout容器中刪除當(dāng)前點擊到的ViewItem
         addHotelNameView.removeView(childAt);
        }
      });
      //如果是最后一個ViewItem,就設(shè)置為添加
      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() + "-----服務(wù)評價:" + hotelEvaluate.getText().toString());
    }
  }
}

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

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

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

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

2.有多條的情況下,如果點擊的是設(shè)置有tag為add標(biāo)記的添加,則添加

如果點擊刪除,在sortHotelViewItem方法里面已經(jīng)設(shè)置過刪除點擊事件,直接從內(nèi)存中刪除,

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

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

以上這篇Android 動態(tài)添加view或item并獲取數(shù)據(jù)的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論