<?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>
<?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>
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());
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有