如何在android studio中单击按钮从LinearLayout中添加和删除CardView?
我有一个包含cardView的线性布局。我想在点击按钮时动态添加和删除CardView。我尝试了下面的代码,但删除按钮不起作用,而且动态添加的视图的填充在添加新视图后会自行更改,如下图所示 ExperienceInfoActivity.java如何在android studio中单击按钮从LinearLayout中添加和删除CardView?,android,android-cardview,Android,Android Cardview,我有一个包含cardView的线性布局。我想在点击按钮时动态添加和删除CardView。我尝试了下面的代码,但删除按钮不起作用,而且动态添加的视图的填充在添加新视图后会自行更改,如下图所示 ExperienceInfoActivity.java package kbg.com.kbgpos.forms; import android.app.DatePickerDialog; import android.content.Context; import android.support.v7.ap
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);
delRowBtn.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);
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();
}
}
}
体验\u详细信息\u row.xml
<?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"
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>
<?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"
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>
活动体验信息.xml
<?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"
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>
<?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"
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>
删除(视图v)中的参数
v
不是要删除的CardView
单击的是
ImageView
在onAddField()中执行以下操作:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.experience_details_row, null);
ImageView delRowBtn = rowView.findViewById(R.id.delRowBtn);
delRowBtn.setTag(rowView);
parentRelativeLayout.addView(rowView, parentRelativeLayout.getChildCount());
CardView experienceInfoActivityFormCardVw = findViewById(R.id.experienceInfoActivityFormCardVw);
ImageView delRowBtn = findViewById(R.id.delRowBtn);
delRowBtn.setTag(experienceInfoActivityFormCardVw);
CardView cv = (CardView) ((ImageView) v).getTag();
parentRelativeLayout.removeView(cv);
上面的代码所做的是将新的cardwiew
对象存储在delRowBtn
的标记中我还从
parentRelativeLayout.getChildCount()
中删除了-1
,以便在末尾添加新的CardView但上述逻辑也必须应用于已经存在的第一个
CardView
,因此在onCreate()
中添加以下内容:LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.experience_details_row, null);
ImageView delRowBtn = rowView.findViewById(R.id.delRowBtn);
delRowBtn.setTag(rowView);
parentRelativeLayout.addView(rowView, parentRelativeLayout.getChildCount());
CardView experienceInfoActivityFormCardVw = findViewById(R.id.experienceInfoActivityFormCardVw);
ImageView delRowBtn = findViewById(R.id.delRowBtn);
delRowBtn.setTag(experienceInfoActivityFormCardVw);
CardView cv = (CardView) ((ImageView) v).getTag();
parentRelativeLayout.removeView(cv);
在
onDelete()中执行以下操作:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.experience_details_row, null);
ImageView delRowBtn = rowView.findViewById(R.id.delRowBtn);
delRowBtn.setTag(rowView);
parentRelativeLayout.addView(rowView, parentRelativeLayout.getChildCount());
CardView experienceInfoActivityFormCardVw = findViewById(R.id.experienceInfoActivityFormCardVw);
ImageView delRowBtn = findViewById(R.id.delRowBtn);
delRowBtn.setTag(experienceInfoActivityFormCardVw);
CardView cv = (CardView) ((ImageView) v).getTag();
parentRelativeLayout.removeView(cv);
上面的代码所做的是从delRowBtn
的标记中检索CardView
对象,并将其从parentRelativeLayout
中删除
活动的完整代码:
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class ExperienceInfoActivity extends AppCompatActivity {
Toolbar toolbar;
private LinearLayout parentRelativeLayout;
private ViewGroup.LayoutParams params;
private int count = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_experience_info);
CardView experienceInfoActivityFormCardVw = findViewById(R.id.experienceInfoActivityFormCardVw);
ImageView delRowBtn = findViewById(R.id.delRowBtn);
delRowBtn.setTag(experienceInfoActivityFormCardVw);
params = experienceInfoActivityFormCardVw.getLayoutParams();
initViews();
initListeners();
}
private void 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);
}
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);
ImageView delRowBtn = rowView.findViewById(R.id.delRowBtn);
delRowBtn.setTag(rowView);
rowView.setLayoutParams(params);
parentRelativeLayout.addView(rowView, parentRelativeLayout.getChildCount());
EditText employerNameEditText = rowView.findViewById(R.id.employerNameEditText);
employerNameEditText.requestFocus();
count++;
}catch (Exception e){
e.printStackTrace();
}
}
public void onDelete(View v) {
try{
if (count == 1) return;
CardView cv = (CardView) ((ImageView) v).getTag();
parentRelativeLayout.removeView(cv);
count--;
}catch (Exception e){
e.printStackTrace();
}
}
}
onDelete(视图v)
中的参数v
不是要删除的cardwiew
单击的是ImageView
在onAddField()中执行以下操作:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.experience_details_row, null);
ImageView delRowBtn = rowView.findViewById(R.id.delRowBtn);
delRowBtn.setTag(rowView);
parentRelativeLayout.addView(rowView, parentRelativeLayout.getChildCount());
CardView experienceInfoActivityFormCardVw = findViewById(R.id.experienceInfoActivityFormCardVw);
ImageView delRowBtn = findViewById(R.id.delRowBtn);
delRowBtn.setTag(experienceInfoActivityFormCardVw);
CardView cv = (CardView) ((ImageView) v).getTag();
parentRelativeLayout.removeView(cv);
上面的代码所做的是将新的cardwiew
对象存储在delRowBtn
的标记中
我还从parentRelativeLayout.getChildCount()
中删除了-1
,以便在末尾添加新的CardView
但上述逻辑也必须应用于已经存在的第一个CardView
,因此在onCreate()
中添加以下内容:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.experience_details_row, null);
ImageView delRowBtn = rowView.findViewById(R.id.delRowBtn);
delRowBtn.setTag(rowView);
parentRelativeLayout.addView(rowView, parentRelativeLayout.getChildCount());
CardView experienceInfoActivityFormCardVw = findViewById(R.id.experienceInfoActivityFormCardVw);
ImageView delRowBtn = findViewById(R.id.delRowBtn);
delRowBtn.setTag(experienceInfoActivityFormCardVw);
CardView cv = (CardView) ((ImageView) v).getTag();
parentRelativeLayout.removeView(cv);
在onDelete()中执行以下操作:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.experience_details_row, null);
ImageView delRowBtn = rowView.findViewById(R.id.delRowBtn);
delRowBtn.setTag(rowView);
parentRelativeLayout.addView(rowView, parentRelativeLayout.getChildCount());
CardView experienceInfoActivityFormCardVw = findViewById(R.id.experienceInfoActivityFormCardVw);
ImageView delRowBtn = findViewById(R.id.delRowBtn);
delRowBtn.setTag(experienceInfoActivityFormCardVw);
CardView cv = (CardView) ((ImageView) v).getTag();
parentRelativeLayout.removeView(cv);
上面的代码所做的是从delRowBtn
的标记中检索CardView
对象,并将其从parentRelativeLayout
中删除
活动的完整代码:
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class ExperienceInfoActivity extends AppCompatActivity {
Toolbar toolbar;
private LinearLayout parentRelativeLayout;
private ViewGroup.LayoutParams params;
private int count = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_experience_info);
CardView experienceInfoActivityFormCardVw = findViewById(R.id.experienceInfoActivityFormCardVw);
ImageView delRowBtn = findViewById(R.id.delRowBtn);
delRowBtn.setTag(experienceInfoActivityFormCardVw);
params = experienceInfoActivityFormCardVw.getLayoutParams();
initViews();
initListeners();
}
private void 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);
}
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);
ImageView delRowBtn = rowView.findViewById(R.id.delRowBtn);
delRowBtn.setTag(rowView);
rowView.setLayoutParams(params);
parentRelativeLayout.addView(rowView, parentRelativeLayout.getChildCount());
EditText employerNameEditText = rowView.findViewById(R.id.employerNameEditText);
employerNameEditText.requestFocus();
count++;
}catch (Exception e){
e.printStackTrace();
}
}
public void onDelete(View v) {
try{
if (count == 1) return;
CardView cv = (CardView) ((ImageView) v).getTag();
parentRelativeLayout.removeView(cv);
count--;
}catch (Exception e){
e.printStackTrace();
}
}
}
你真了不起@mTak。我真的很感谢你。谢谢我已经尝试了上面的代码,添加和删除行的逻辑问题已经解决,尽管填充问题仍然存在。请帮我做这个。另外,请让我知道,当添加新的cardview时,如何将焦点移动到新的cardview?我认为现在填充和焦点问题已经解决。我发布了完整的代码。试试看。我补充了一个建议:记录卡片的数量,这样如果你只有一张,它就不会被删除。如果您不需要它,只需删除与变量count
有关的所有内容。根据您的建议,我已经编辑了我的代码,您可以在上面看到。但现在我试图实现的是,当我点击“添加”按钮时,它应该从当前卡中删除,并且应该添加到新卡中,我可以这样做,但当只剩下一张卡时,我无法再次将“添加行”按钮添加到单个左卡中,plz帮助。另外,我在fromDateEditText上添加了datepicker,但它只适用于第一张卡,不适用于动态添加的CardView。你能给我引路吗?第一部分我真的不明白你想要什么。第二部分:随着你的应用程序需求的增长,我必须告诉你你的方法是错误的。您应该使用类似recyclerview的列表,而不是添加或删除的单个项目。所有这些您想要添加到项目中的新操作都将变得越来越困难,因为对于每一个操作,您都必须执行两次。一次用于第一项,另一次用于所有其他项。所以现在你还处于开始阶段,我建议你重新思考和重新设计。正是在这些情况下实现了recyclerview。您真是太棒了@mTak。我真的很感谢你。谢谢我已经尝试了上面的代码,添加和删除行的逻辑问题已经解决,尽管填充问题仍然存在。请帮我做这个。另外,请让我知道,当添加新的cardview时,如何将焦点移动到新的cardview?我认为现在填充和焦点问题已经解决。我发布了完整的代码。试试看。我补充了一个建议:记录卡片的数量,这样如果你只有一张,它就不会被删除。如果您不需要它,只需删除与变量count
有关的所有内容。根据您的建议,我已经编辑了我的代码,您可以在上面看到。但现在我试图实现的是,当我点击“添加”按钮时,它应该从当前卡中删除,并且应该添加到新卡中,我可以这样做,但当只剩下一张卡时,我无法再次将“添加行”按钮添加到单个左卡中,plz帮助。另外,我在fromDateEditText上添加了datepicker,但它只适用于第一张卡,不适用于动态添加的CardView。你能给我引路吗?第一部分我真的不明白你想要什么