模擬酒店預訂介面新增聯絡人的功能
阿新 • • 發佈:2018-12-14
專案中需要做一個酒店預訂的介面,相關功能類似百度糯米里面的頁面,難點主要是動態新增入住人,涉及的知識點主要是動態新增多個View和隱藏多個View。此處只是一個小Demo,僅供參考!
主介面程式碼:
package com.src.bingley.createview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.Switch; import android.widget.TextView; import java.lang.reflect.Array; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button mClick; private LinearLayout container; private RelativeLayout planel; private TextView mShow; private Button mDelete; private static int MaxView = 10; private static int DefView = 1; private boolean state = false; private static Integer[] mArrayId = { R.id.text_view_1, R.id.text_view_2, R.id.text_view_3, R.id.text_view_4, R.id.text_view_5, R.id.text_view_6, R.id.text_view_7, R.id.text_view_8, R.id.text_view_9, R.id.text_view_10 }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mClick = (Button) findViewById(R.id.add_btn); mDelete = (Button) findViewById(R.id.del_btn); mClick.setOnClickListener(this); mDelete.setOnClickListener(this); container = (LinearLayout) findViewById(R.id.guest_name); mShow = (TextView) findViewById(R.id.showhide); planel = (RelativeLayout) findViewById(R.id.namePanel); CreateView(MaxView); ShowView(DefView); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.add_btn: ShowView(3); if (!state) { mShow.setVisibility(View.VISIBLE); state = true; } else { mShow.setVisibility(View.GONE); state = false; } break; case R.id.del_btn: ShowView(5); break; default: break; } } public void CreateView(int count) { for (int i = 0; i < count; i++) { LayoutInflater inflater = this.getLayoutInflater(); RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.nameitem, null); view.setId(mArrayId[i]); container.addView(view); } } public void ShowView(int count) { for (int i = 0; i < count; i++) { View showView = (View) findViewById(mArrayId[i]); if (showView.getVisibility() == View.GONE) { showView.setVisibility(View.VISIBLE); } } for (int j = count; j < MaxView; j++) { View hideView = (View) findViewById(mArrayId[j]); if (hideView.getVisibility() == View.VISIBLE) { hideView.setVisibility(View.GONE); } } } }
佈局activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <ScrollView 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" tools:context="com.src.bingley.createview.MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/namePanel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp"> <TextView android:id="@+id/summary_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:background="#BBDD22" android:gravity="center_vertical" android:text="入住人" android:textSize="20sp" /> <LinearLayout android:id="@+id/guest_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="30dp" android:layout_toRightOf="@id/summary_name" android:orientation="vertical"> </LinearLayout> </RelativeLayout> <View android:id="@+id/divider" android:layout_width="match_parent" android:layout_height="1px" android:layout_below="@id/namePanel" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="#000000" /> <Button android:id="@+id/add_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/divider" android:layout_marginTop="10dp" android:gravity="center" android:text="新增3個入住人" android:textSize="20sp" /> <Button android:id="@+id/del_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/add_btn" android:gravity="center" android:text="新增5個入住人" android:textSize="20sp" /> <TextView android:id="@+id/showhide" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/del_btn" android:gravity="center" android:text="模擬酒店預定頁面新增聯絡人" android:textSize="30dp" /> </RelativeLayout> </ScrollView>
動態新增的Item(nameitem.xml):
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/editname" android:background="#CCDD33" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_centerVertical="true" android:textSize="20sp" android:gravity="center" android:hint="入住人姓名"/> <ImageView android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_marginRight="10dp" android:src="@drawable/delete" android:scaleType="fitCenter"/> <View android:id="@+id/dividerItem" android:layout_width="match_parent" android:layout_height="1px" android:layout_below="@id/delete" android:background="#000000" /> </RelativeLayout>
動態設定的ViewId(ids.xml):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="text_view_1" type="id">text_view_1</item>
<item name="text_view_2" type="id">text_view_2</item>
<item name="text_view_3" type="id">text_view_3</item>
<item name="text_view_4" type="id">text_view_4</item>
<item name="text_view_5" type="id">text_view_5</item>
<item name="text_view_6" type="id">text_view_6</item>
<item name="text_view_7" type="id">text_view_7</item>
<item name="text_view_8" type="id">text_view_8</item>
<item name="text_view_9" type="id">text_view_9</item>
<item name="text_view_10" type="id">text_view_10</item>
</resources>