如何在从 LinearLayout 中删除第二个 cardview 时向第一个 cardview 添加 Add_button,即当线性布局中要留下的子级数量是?
How to add a Add_button to first cardview on deleting second cardview from LinearLayout i.e. when number of children to be left in Linear Layout is?
我想要实现的是,当我点击 addRowBtn
(+) 时,一个新的卡片视图被添加到线性布局和添加按钮 ( +) 以前的卡片视图被删除。我面临的挑战是当我在线性布局中有 2 张卡片并且我删除了第二个 cardView 时,我想再次将该添加按钮 (+) 添加到前一个,即第一个 cardView 留在线性布局。
此外,我有两个按钮,fromDateEditText
和 toDateEditText
,我想为其打开日期选择器,但它为第一个 cardView 打开,而不是为动态添加的 cardView 打开。
请帮我解决这个问题。
ExperienceInfoActivity
package kbg.com.kbgpos.forms;
import android.app.DatePickerDialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import kbg.com.kbgpos.R;
public class ExperienceInfoActivity extends AppCompatActivity {
Toolbar toolbar;
private LinearLayout parentRelativeLayout;
private View v;
EditText fromDateEditText,toDateEditText;
private ViewGroup.LayoutParams params;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_experience_info);
initViews();
initListeners();
}
private void initViews() {
toolbar=(Toolbar) findViewById(R.id.toolbarExperienceInfoActivity);
toolbar.setTitle("Employee Experience Info");
toolbar.setTitleTextColor(getResources().getColor(R.color.toolBarTitle));
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_black_24dp));
setSupportActionBar(toolbar);
parentRelativeLayout = (LinearLayout) findViewById(R.id.experienceDetailsInfoRelLayout);
CardView experienceInfoActivityFormCardVw = findViewById(R.id.experienceInfoActivityFormCardVw);
ImageView delRowBtn = findViewById(R.id.delRowBtn);
ImageView addRowBtn = findViewById(R.id.addRowBtn);
delRowBtn.setTag(experienceInfoActivityFormCardVw);
addRowBtn.setTag(experienceInfoActivityFormCardVw);
params = experienceInfoActivityFormCardVw.getLayoutParams();
fromDateEditText=(EditText)findViewById(R.id.fromDateEditText);
toDateEditText=(EditText)findViewById(R.id.toDateEditText);
}
private void initListeners() {
fromDateEditText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= fromDateEditText.getRight() - fromDateEditText.getTotalPaddingRight()) {
DatePickerDialog.OnDateSetListener dpd = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
int s=monthOfYear+1;
String a = dayOfMonth+"/"+s+"/"+year;
fromDateEditText.setText(""+a);
}
};
Time date = new Time();
DatePickerDialog d = new DatePickerDialog(ExperienceInfoActivity.this, dpd, date.year ,date.month, date.monthDay);
d.show();
return true;
}
}
return true;
}
});
toDateEditText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= toDateEditText.getRight() - toDateEditText.getTotalPaddingRight()) {
DatePickerDialog.OnDateSetListener dpd = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
int s=monthOfYear+1;
String a = dayOfMonth+"/"+s+"/"+year;
toDateEditText.setText(""+a);
}
};
Time date = new Time();
DatePickerDialog d = new DatePickerDialog(ExperienceInfoActivity.this, dpd, date.year ,date.month, date.monthDay);
d.show();
return true;
}
}
return true;
}
});
}
public void onAddField(View view) {
try{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.experience_details_row, null);
if(parentRelativeLayout.getChildCount()>1){
ViewGroup parent = (ViewGroup) view.getParent();
parent.removeView(view);
}
ImageView delRowBtn = rowView.findViewById(R.id.delRowBtn);
delRowBtn.setTag(rowView);
ImageView addRowBtn=rowView.findViewById(R.id.addRowBtn);
addRowBtn.setTag(rowView);
rowView.setLayoutParams(params);
parentRelativeLayout.addView(rowView, parentRelativeLayout.getChildCount());
EditText employerNameEditText = rowView.findViewById(R.id.employerNameEditText);
employerNameEditText.requestFocus();
}catch (Exception e){
e.printStackTrace();
}
}
public void onDelete(View v) {
try{
if(parentRelativeLayout.getChildCount()>2) {
CardView cv = (CardView) ((ImageView) v).getTag();
parentRelativeLayout.removeView(cv);
}else{
ViewGroup parent = (ViewGroup) v.getParent();
v.setBackgroundColor(getResources().getColor(R.color.material_grey_50));
}
}catch (Exception e){
e.printStackTrace();
}
}
}
activity_experience_info
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".forms.ExperienceInfoActivity">
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="@layout/toolbar_layout"
android:id="@+id/toolbarExperienceInfoActivity"></include>
<ScrollView
android:id="@+id/personalDetailScroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:layout_marginBottom="55dp"
android:scrollbars = "vertical"
android:layout_below="@id/toolbarExperienceInfoActivity">
<LinearLayout
android:id="@+id/experienceDetailsInfoRelLayout"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/experienceInfoActivityFormHeadingCardVw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="10dp"
card_view:cardElevation="2dp"
card_view:contentPadding="5dp"
card_view:cardCornerRadius="2dp"
app:cardBackgroundColor="@android:color/holo_orange_light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Experience Details"
android:textStyle="bold"
android:textColor="@android:color/white"
android:layout_gravity="center"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/experienceInfoActivityFormCardVw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
app:cardUseCompatPadding="true"
card_view:cardElevation="2dp"
card_view:contentPadding="5dp"
card_view:cardCornerRadius="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:id="@+id/employerNameTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@style/text_in_layout_hint_Style"
app:errorTextAppearance="@style/text_in_layout_error_hint_Style">
<EditText
android:id="@+id/employerNameEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:textSize="12sp"
android:inputType="textPersonName"
android:hint="Employer Name" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/designationTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="@style/text_in_layout_hint_Style"
app:errorTextAppearance="@style/text_in_layout_error_hint_Style"
android:layout_below="@id/employerNameTextInputLayout">
<EditText
android:id="@+id/designationEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:textSize="12sp"
android:inputType="textPersonName"
android:hint="Designation" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/addressTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="@style/text_in_layout_hint_Style"
app:errorTextAppearance="@style/text_in_layout_error_hint_Style"
android:layout_below="@id/designationTextInputLayout">
<EditText
android:id="@+id/addressEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:textSize="12sp"
android:inputType="textPersonName"
android:hint="Address" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/fromDateTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="@style/text_in_layout_hint_Style"
app:errorTextAppearance="@style/text_in_layout_error_hint_Style"
android:layout_below="@id/addressTextInputLayout">
<EditText
android:id="@+id/fromDateEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="12sp"
android:inputType="date"
android:drawableRight="@drawable/ic_perm_contact_calendar_black_24dp"
android:drawableTint="@android:color/holo_orange_light"
android:hint="From Date" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/toDateTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="@style/text_in_layout_hint_Style"
app:errorTextAppearance="@style/text_in_layout_error_hint_Style"
android:layout_below="@id/fromDateTextInputLayout">
<EditText
android:id="@+id/toDateEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="12sp"
android:inputType="date"
android:hint="To Date"
android:drawableRight="@drawable/ic_perm_contact_calendar_black_24dp"
android:drawableTint="@android:color/holo_orange_light"/>
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:id="@+id/addDelLayout"
android:layout_marginTop="5dp"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="horizontal"
android:layout_below="@id/toDateTextInputLayout">
<ImageView
android:id="@+id/addRowBtn"
android:src="@drawable/ic_add_box_black_24dp"
android:layout_width="40dp"
android:layout_height="30dp"
android:onClick="onAddField"
android:background="@android:color/holo_green_light"
android:layout_marginRight="30dp"/>
<ImageView
android:id="@+id/delRowBtn"
android:src="@drawable/ic_delete_black_24dp"
android:background="@android:color/holo_red_dark"
android:layout_width="40dp"
android:layout_height="30dp"
android:onClick="onDelete"/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_alignParentBottom="true"
android:elevation="4dp"
android:layout_width="match_parent"
android:layout_height="55dp"
android:padding="5dp"
android:weightSum="2"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/cancelBtn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="45dp"
android:text="CANCEL"
android:background="#e0e0e0"
android:textStyle="bold"
android:textColor="@android:color/white"/>
<Button
android:id="@+id/saveBtn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="45dp"
android:text="SUBMIT"
android:background="#ffe57f"
android:textStyle="bold"
android:textColor="@android:color/white"/>
</LinearLayout>
</RelativeLayout>
experience_details_row
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/experienceInfoActivityFormCardVw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
app:cardUseCompatPadding="true"
card_view:cardElevation="2dp"
card_view:contentPadding="5dp"
card_view:cardCornerRadius="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:id="@+id/employerNameTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@style/text_in_layout_hint_Style"
app:errorTextAppearance="@style/text_in_layout_error_hint_Style">
<EditText
android:id="@+id/employerNameEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:textSize="12sp"
android:inputType="textPersonName"
android:hint="Employer Name" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/designationTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="@style/text_in_layout_hint_Style"
app:errorTextAppearance="@style/text_in_layout_error_hint_Style"
android:layout_below="@id/employerNameTextInputLayout">
<EditText
android:id="@+id/designationEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:textSize="12sp"
android:inputType="textPersonName"
android:hint="Designation" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/addressTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="@style/text_in_layout_hint_Style"
app:errorTextAppearance="@style/text_in_layout_error_hint_Style"
android:layout_below="@id/designationTextInputLayout">
<EditText
android:id="@+id/addressEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:textSize="12sp"
android:inputType="textPersonName"
android:hint="Address" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/fromDateTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="@style/text_in_layout_hint_Style"
app:errorTextAppearance="@style/text_in_layout_error_hint_Style"
android:layout_below="@id/addressTextInputLayout">
<EditText
android:id="@+id/fromDateEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="12sp"
android:inputType="date"
android:drawableRight="@drawable/ic_perm_contact_calendar_black_24dp"
android:drawableTint="@android:color/holo_orange_light"
android:hint="From Date" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/toDateTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="@style/text_in_layout_hint_Style"
app:errorTextAppearance="@style/text_in_layout_error_hint_Style"
android:layout_below="@id/fromDateTextInputLayout">
<EditText
android:id="@+id/toDateEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="12sp"
android:inputType="date"
android:hint="To Date"
android:drawableRight="@drawable/ic_perm_contact_calendar_black_24dp"
android:drawableTint="@android:color/holo_orange_light"/>
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:id="@+id/addDelLayout"
android:layout_marginTop="5dp"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="horizontal"
android:layout_below="@id/toDateTextInputLayout">
<ImageView
android:id="@+id/addRowBtn"
android:src="@drawable/ic_add_box_black_24dp"
android:layout_width="40dp"
android:layout_height="30dp"
android:onClick="onAddField"
android:background="@android:color/holo_green_light"
android:layout_marginRight="30dp"/>
<ImageView
android:id="@+id/delRowBtn"
android:src="@drawable/ic_delete_black_24dp"
android:background="@android:color/holo_red_dark"
android:layout_width="40dp"
android:layout_height="30dp"
android:onClick="onDelete"/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
我也附上了图片:
显示的图像描绘了两种状态:添加第二张卡和删除第二张卡。
Disclaimer: this is not the best way to code such layout use
RecyclerView.
您可以使用 getChildAt(index) 获取父布局中特定索引处的子元素。
public void onAddField(View view) {
/* first make add button invisible on which click operation is performed
*/
view.getVisibility(View.GONE);
try{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.experience_details_row, null);
//get reference to buttons here form inflated view
EditText fromDateEditTextDynamic=(EditText)rowView.findViewById(R.id.fromDateEditText);
EditText toDateEditTextDynamic=(EditText)rowView.findViewById(R.id.toDateEditText);
//apply listeners
fromDateEditTextDynamic.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= fromDateEditTextDynamic.getRight() - fromDateEditTextDynamic.getTotalPaddingRight()) {
DatePickerDialog.OnDateSetListener dpd = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
int s=monthOfYear+1;
String a = dayOfMonth+"/"+s+"/"+year;
fromDateEditTextDynamic.setText(""+a);
}
};
Time date = new Time();
DatePickerDialog d = new DatePickerDialog(ExperienceInfoActivity.this, dpd, date.year ,date.month, date.monthDay);
d.show();
return true;
}
}
return true;
}
});
toDateEditTextDynamic.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= toDateEditTextDynamic.getRight() - toDateEditTextDynamic.getTotalPaddingRight()) {
DatePickerDialog.OnDateSetListener dpd = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
int s=monthOfYear+1;
String a = dayOfMonth+"/"+s+"/"+year;
toDateEditTextDynamic.setText(""+a);
}
};
Time date = new Time();
DatePickerDialog d = new DatePickerDialog(ExperienceInfoActivity.this, dpd, date.year ,date.month, date.monthDay);
d.show();
return true;
}
}
return true;
}
});
ImageView delRowBtn = rowView.findViewById(R.id.delRowBtn);
delRowBtn.setTag(rowView);
ImageView addRowBtn=rowView.findViewById(R.id.addRowBtn);
addRowBtn.setTag(rowView);
rowView.setLayoutParams(params);
parentRelativeLayout.addView(rowView, parentRelativeLayout.getChildCount());
EditText employerNameEditText = rowView.findViewById(R.id.employerNameEditText);
employerNameEditText.requestFocus();
}catch (Exception e){
e.printStackTrace();
}
}
然后在代码中替换这些行
try{
if(parentRelativeLayout.getChildCount()>2) {
CardView cv = (CardView) ((ImageView) v).getTag();
parentRelativeLayout.removeView(cv);
}else{
ViewGroup parent = (ViewGroup) v.getParent();
v.setBackgroundColor(getResources().getColor(R.color.material_grey_50));
}
}catch (Exception e){
e.printStackTrace();
}
使用下面的代码将使 addbtn 对之前添加的每个 CardView 都可见。
try{
if(parentRelativeLayout.getChildCount()>2) {
int indexPreviousCardView = parentRelativeLayout.getChildCount()-2;
if(indexPreviousCardView > -1){
CardView previousCardView = parentRelativeLayout.getChildAt(indexPreviousCardView);
//now get the addBtn from previous cardView and set its visibility
// as it was gone as we added new row so set its visibility to visible
ImageView addBtn = (ImageView) previousCardView.findViewById(R.id.addRowBtn);
addBtn.setVisibility(View.VISIBLE);
}
CardView cv = (CardView) ((ImageView) v).getTag();
parentRelativeLayout.removeView(cv);
}
else{
ViewGroup parent = (ViewGroup) v.getParent();
v.setBackgroundColor(getResources().getColor(R.color.material_grey_50));
}
}catch(Exception e){
e.printStackTrace();
}
我想要实现的是,当我点击 addRowBtn
(+) 时,一个新的卡片视图被添加到线性布局和添加按钮 ( +) 以前的卡片视图被删除。我面临的挑战是当我在线性布局中有 2 张卡片并且我删除了第二个 cardView 时,我想再次将该添加按钮 (+) 添加到前一个,即第一个 cardView 留在线性布局。
此外,我有两个按钮,fromDateEditText
和 toDateEditText
,我想为其打开日期选择器,但它为第一个 cardView 打开,而不是为动态添加的 cardView 打开。
请帮我解决这个问题。
ExperienceInfoActivity
package kbg.com.kbgpos.forms;
import android.app.DatePickerDialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import kbg.com.kbgpos.R;
public class ExperienceInfoActivity extends AppCompatActivity {
Toolbar toolbar;
private LinearLayout parentRelativeLayout;
private View v;
EditText fromDateEditText,toDateEditText;
private ViewGroup.LayoutParams params;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_experience_info);
initViews();
initListeners();
}
private void initViews() {
toolbar=(Toolbar) findViewById(R.id.toolbarExperienceInfoActivity);
toolbar.setTitle("Employee Experience Info");
toolbar.setTitleTextColor(getResources().getColor(R.color.toolBarTitle));
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_black_24dp));
setSupportActionBar(toolbar);
parentRelativeLayout = (LinearLayout) findViewById(R.id.experienceDetailsInfoRelLayout);
CardView experienceInfoActivityFormCardVw = findViewById(R.id.experienceInfoActivityFormCardVw);
ImageView delRowBtn = findViewById(R.id.delRowBtn);
ImageView addRowBtn = findViewById(R.id.addRowBtn);
delRowBtn.setTag(experienceInfoActivityFormCardVw);
addRowBtn.setTag(experienceInfoActivityFormCardVw);
params = experienceInfoActivityFormCardVw.getLayoutParams();
fromDateEditText=(EditText)findViewById(R.id.fromDateEditText);
toDateEditText=(EditText)findViewById(R.id.toDateEditText);
}
private void initListeners() {
fromDateEditText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= fromDateEditText.getRight() - fromDateEditText.getTotalPaddingRight()) {
DatePickerDialog.OnDateSetListener dpd = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
int s=monthOfYear+1;
String a = dayOfMonth+"/"+s+"/"+year;
fromDateEditText.setText(""+a);
}
};
Time date = new Time();
DatePickerDialog d = new DatePickerDialog(ExperienceInfoActivity.this, dpd, date.year ,date.month, date.monthDay);
d.show();
return true;
}
}
return true;
}
});
toDateEditText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= toDateEditText.getRight() - toDateEditText.getTotalPaddingRight()) {
DatePickerDialog.OnDateSetListener dpd = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
int s=monthOfYear+1;
String a = dayOfMonth+"/"+s+"/"+year;
toDateEditText.setText(""+a);
}
};
Time date = new Time();
DatePickerDialog d = new DatePickerDialog(ExperienceInfoActivity.this, dpd, date.year ,date.month, date.monthDay);
d.show();
return true;
}
}
return true;
}
});
}
public void onAddField(View view) {
try{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.experience_details_row, null);
if(parentRelativeLayout.getChildCount()>1){
ViewGroup parent = (ViewGroup) view.getParent();
parent.removeView(view);
}
ImageView delRowBtn = rowView.findViewById(R.id.delRowBtn);
delRowBtn.setTag(rowView);
ImageView addRowBtn=rowView.findViewById(R.id.addRowBtn);
addRowBtn.setTag(rowView);
rowView.setLayoutParams(params);
parentRelativeLayout.addView(rowView, parentRelativeLayout.getChildCount());
EditText employerNameEditText = rowView.findViewById(R.id.employerNameEditText);
employerNameEditText.requestFocus();
}catch (Exception e){
e.printStackTrace();
}
}
public void onDelete(View v) {
try{
if(parentRelativeLayout.getChildCount()>2) {
CardView cv = (CardView) ((ImageView) v).getTag();
parentRelativeLayout.removeView(cv);
}else{
ViewGroup parent = (ViewGroup) v.getParent();
v.setBackgroundColor(getResources().getColor(R.color.material_grey_50));
}
}catch (Exception e){
e.printStackTrace();
}
}
}
activity_experience_info
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".forms.ExperienceInfoActivity">
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="@layout/toolbar_layout"
android:id="@+id/toolbarExperienceInfoActivity"></include>
<ScrollView
android:id="@+id/personalDetailScroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:layout_marginBottom="55dp"
android:scrollbars = "vertical"
android:layout_below="@id/toolbarExperienceInfoActivity">
<LinearLayout
android:id="@+id/experienceDetailsInfoRelLayout"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/experienceInfoActivityFormHeadingCardVw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="10dp"
card_view:cardElevation="2dp"
card_view:contentPadding="5dp"
card_view:cardCornerRadius="2dp"
app:cardBackgroundColor="@android:color/holo_orange_light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Experience Details"
android:textStyle="bold"
android:textColor="@android:color/white"
android:layout_gravity="center"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/experienceInfoActivityFormCardVw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
app:cardUseCompatPadding="true"
card_view:cardElevation="2dp"
card_view:contentPadding="5dp"
card_view:cardCornerRadius="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:id="@+id/employerNameTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@style/text_in_layout_hint_Style"
app:errorTextAppearance="@style/text_in_layout_error_hint_Style">
<EditText
android:id="@+id/employerNameEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:textSize="12sp"
android:inputType="textPersonName"
android:hint="Employer Name" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/designationTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="@style/text_in_layout_hint_Style"
app:errorTextAppearance="@style/text_in_layout_error_hint_Style"
android:layout_below="@id/employerNameTextInputLayout">
<EditText
android:id="@+id/designationEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:textSize="12sp"
android:inputType="textPersonName"
android:hint="Designation" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/addressTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="@style/text_in_layout_hint_Style"
app:errorTextAppearance="@style/text_in_layout_error_hint_Style"
android:layout_below="@id/designationTextInputLayout">
<EditText
android:id="@+id/addressEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:textSize="12sp"
android:inputType="textPersonName"
android:hint="Address" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/fromDateTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="@style/text_in_layout_hint_Style"
app:errorTextAppearance="@style/text_in_layout_error_hint_Style"
android:layout_below="@id/addressTextInputLayout">
<EditText
android:id="@+id/fromDateEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="12sp"
android:inputType="date"
android:drawableRight="@drawable/ic_perm_contact_calendar_black_24dp"
android:drawableTint="@android:color/holo_orange_light"
android:hint="From Date" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/toDateTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="@style/text_in_layout_hint_Style"
app:errorTextAppearance="@style/text_in_layout_error_hint_Style"
android:layout_below="@id/fromDateTextInputLayout">
<EditText
android:id="@+id/toDateEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="12sp"
android:inputType="date"
android:hint="To Date"
android:drawableRight="@drawable/ic_perm_contact_calendar_black_24dp"
android:drawableTint="@android:color/holo_orange_light"/>
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:id="@+id/addDelLayout"
android:layout_marginTop="5dp"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="horizontal"
android:layout_below="@id/toDateTextInputLayout">
<ImageView
android:id="@+id/addRowBtn"
android:src="@drawable/ic_add_box_black_24dp"
android:layout_width="40dp"
android:layout_height="30dp"
android:onClick="onAddField"
android:background="@android:color/holo_green_light"
android:layout_marginRight="30dp"/>
<ImageView
android:id="@+id/delRowBtn"
android:src="@drawable/ic_delete_black_24dp"
android:background="@android:color/holo_red_dark"
android:layout_width="40dp"
android:layout_height="30dp"
android:onClick="onDelete"/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_alignParentBottom="true"
android:elevation="4dp"
android:layout_width="match_parent"
android:layout_height="55dp"
android:padding="5dp"
android:weightSum="2"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/cancelBtn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="45dp"
android:text="CANCEL"
android:background="#e0e0e0"
android:textStyle="bold"
android:textColor="@android:color/white"/>
<Button
android:id="@+id/saveBtn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="45dp"
android:text="SUBMIT"
android:background="#ffe57f"
android:textStyle="bold"
android:textColor="@android:color/white"/>
</LinearLayout>
</RelativeLayout>
experience_details_row
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/experienceInfoActivityFormCardVw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
app:cardUseCompatPadding="true"
card_view:cardElevation="2dp"
card_view:contentPadding="5dp"
card_view:cardCornerRadius="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:id="@+id/employerNameTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@style/text_in_layout_hint_Style"
app:errorTextAppearance="@style/text_in_layout_error_hint_Style">
<EditText
android:id="@+id/employerNameEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:textSize="12sp"
android:inputType="textPersonName"
android:hint="Employer Name" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/designationTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="@style/text_in_layout_hint_Style"
app:errorTextAppearance="@style/text_in_layout_error_hint_Style"
android:layout_below="@id/employerNameTextInputLayout">
<EditText
android:id="@+id/designationEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:textSize="12sp"
android:inputType="textPersonName"
android:hint="Designation" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/addressTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="@style/text_in_layout_hint_Style"
app:errorTextAppearance="@style/text_in_layout_error_hint_Style"
android:layout_below="@id/designationTextInputLayout">
<EditText
android:id="@+id/addressEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:textSize="12sp"
android:inputType="textPersonName"
android:hint="Address" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/fromDateTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="@style/text_in_layout_hint_Style"
app:errorTextAppearance="@style/text_in_layout_error_hint_Style"
android:layout_below="@id/addressTextInputLayout">
<EditText
android:id="@+id/fromDateEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="12sp"
android:inputType="date"
android:drawableRight="@drawable/ic_perm_contact_calendar_black_24dp"
android:drawableTint="@android:color/holo_orange_light"
android:hint="From Date" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/toDateTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:hintTextAppearance="@style/text_in_layout_hint_Style"
app:errorTextAppearance="@style/text_in_layout_error_hint_Style"
android:layout_below="@id/fromDateTextInputLayout">
<EditText
android:id="@+id/toDateEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="12sp"
android:inputType="date"
android:hint="To Date"
android:drawableRight="@drawable/ic_perm_contact_calendar_black_24dp"
android:drawableTint="@android:color/holo_orange_light"/>
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:id="@+id/addDelLayout"
android:layout_marginTop="5dp"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="horizontal"
android:layout_below="@id/toDateTextInputLayout">
<ImageView
android:id="@+id/addRowBtn"
android:src="@drawable/ic_add_box_black_24dp"
android:layout_width="40dp"
android:layout_height="30dp"
android:onClick="onAddField"
android:background="@android:color/holo_green_light"
android:layout_marginRight="30dp"/>
<ImageView
android:id="@+id/delRowBtn"
android:src="@drawable/ic_delete_black_24dp"
android:background="@android:color/holo_red_dark"
android:layout_width="40dp"
android:layout_height="30dp"
android:onClick="onDelete"/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
我也附上了图片:
显示的图像描绘了两种状态:添加第二张卡和删除第二张卡。
Disclaimer: this is not the best way to code such layout use RecyclerView.
您可以使用 getChildAt(index) 获取父布局中特定索引处的子元素。
public void onAddField(View view) {
/* first make add button invisible on which click operation is performed
*/
view.getVisibility(View.GONE);
try{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.experience_details_row, null);
//get reference to buttons here form inflated view
EditText fromDateEditTextDynamic=(EditText)rowView.findViewById(R.id.fromDateEditText);
EditText toDateEditTextDynamic=(EditText)rowView.findViewById(R.id.toDateEditText);
//apply listeners
fromDateEditTextDynamic.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= fromDateEditTextDynamic.getRight() - fromDateEditTextDynamic.getTotalPaddingRight()) {
DatePickerDialog.OnDateSetListener dpd = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
int s=monthOfYear+1;
String a = dayOfMonth+"/"+s+"/"+year;
fromDateEditTextDynamic.setText(""+a);
}
};
Time date = new Time();
DatePickerDialog d = new DatePickerDialog(ExperienceInfoActivity.this, dpd, date.year ,date.month, date.monthDay);
d.show();
return true;
}
}
return true;
}
});
toDateEditTextDynamic.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= toDateEditTextDynamic.getRight() - toDateEditTextDynamic.getTotalPaddingRight()) {
DatePickerDialog.OnDateSetListener dpd = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
int s=monthOfYear+1;
String a = dayOfMonth+"/"+s+"/"+year;
toDateEditTextDynamic.setText(""+a);
}
};
Time date = new Time();
DatePickerDialog d = new DatePickerDialog(ExperienceInfoActivity.this, dpd, date.year ,date.month, date.monthDay);
d.show();
return true;
}
}
return true;
}
});
ImageView delRowBtn = rowView.findViewById(R.id.delRowBtn);
delRowBtn.setTag(rowView);
ImageView addRowBtn=rowView.findViewById(R.id.addRowBtn);
addRowBtn.setTag(rowView);
rowView.setLayoutParams(params);
parentRelativeLayout.addView(rowView, parentRelativeLayout.getChildCount());
EditText employerNameEditText = rowView.findViewById(R.id.employerNameEditText);
employerNameEditText.requestFocus();
}catch (Exception e){
e.printStackTrace();
}
}
然后在代码中替换这些行
try{
if(parentRelativeLayout.getChildCount()>2) {
CardView cv = (CardView) ((ImageView) v).getTag();
parentRelativeLayout.removeView(cv);
}else{
ViewGroup parent = (ViewGroup) v.getParent();
v.setBackgroundColor(getResources().getColor(R.color.material_grey_50));
}
}catch (Exception e){
e.printStackTrace();
}
使用下面的代码将使 addbtn 对之前添加的每个 CardView 都可见。
try{
if(parentRelativeLayout.getChildCount()>2) {
int indexPreviousCardView = parentRelativeLayout.getChildCount()-2;
if(indexPreviousCardView > -1){
CardView previousCardView = parentRelativeLayout.getChildAt(indexPreviousCardView);
//now get the addBtn from previous cardView and set its visibility
// as it was gone as we added new row so set its visibility to visible
ImageView addBtn = (ImageView) previousCardView.findViewById(R.id.addRowBtn);
addBtn.setVisibility(View.VISIBLE);
}
CardView cv = (CardView) ((ImageView) v).getTag();
parentRelativeLayout.removeView(cv);
}
else{
ViewGroup parent = (ViewGroup) v.getParent();
v.setBackgroundColor(getResources().getColor(R.color.material_grey_50));
}
}catch(Exception e){
e.printStackTrace();
}