我是android开发人员的新手,片段给我带来了麻烦.
基本上,我制作的应用程序要求用户每周输入他的时间表(学校),为期6天.因此,我每天都制作了一个片段(即6个片段),而且每个片段的末尾都有一个添加主题按钮底部,在单击时添加xml布局.我已经想出了如何使用xml文件中的布局在片段中添加edittext字段和两个按钮(一个称为data,另一个称为delete),现在单击Delete按钮,我希望删除该特定行.我该怎么做呢?
public class NewTiMetableMonday extends Fragment {
Context context; // context is needed to use inflater outside on create view
View view; // view needs to be passed for (find view by id)
private int unique_id = 0; // for dynamic Id allocation when New Fields are created
private final List<TextView> SUBJECT_NAME = new ArrayList<TextView>();
public NewTiMetableMonday() {
// required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_new_time_table_monday,
container, false);
context = container.getContext();
// adding two entries by default
AddNewSubjectLine(view);
AddNewSubjectLine(view);
// for button +Add Another
Button button_AddAnother = (Button) view.findViewById(R.id.AddAnother);
button_AddAnother.setonClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AddNewSubjectLine(view);
// Adds another Subject Entry Line on the button being clicked
}
});
// for button x
Button button_crossDelete = (Button) view.findViewById(R.id.Delete);
button_crossDelete.setonClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
DeleteSubjectLine(v);
// Deletes that particular line when x is clicked
}
});
// For Button Done
Button button_Done = (Button) view.findViewById(R.id.Done);
button_Done.setonClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// viewPager.setCurrentItem passes the arg0 value to getItem
// to move to page 2 when done is clicked
CreateNewTiMetable.viewPager.setCurrentItem(1, true);
}
});
return view;// because OnCreatView is of type View
}
这是addsubjectline函数
public void AddNewSubjectLine(View view) {
// To display another SUbject Entry Line
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View subjectLayout = inflater.inflate(R.layout.new_entry_line, null);
LinearLayout layout = (LinearLayout) view
.findViewById(R.id.MondayLayout);
layout.addView(subjectLayout);
/** Adding to list **/
// Subject Name
EditText subName = (EditText) view.findViewById(R.id.SubName);
subName.setId(unique_id);
SUBJECT_NAME.add(subName);
// Time Button
Button button_EnterTime = (Button) view.findViewById(R.id.Time);
button_EnterTime.setId(100 + unique_id);
// Delete button
Button deleteSub = (Button) view.findViewById(R.id.Delete);
deleteSub.setId(200 + unique_id);
final ScrollView scrollView = (ScrollView) view
.findViewById(R.id.scrollViewMonday);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
}, 400);
unique_id++;
}
和使用的两个XML文件
这是fragment_new_time_table_monday.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textViewmonday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/monday"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:id="@+id/linearLayoutmonday"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" >
<Button
android:id="@+id/AddAnother"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/add" />
<Button
android:id="@+id/Done"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/done" />
</LinearLayout>
<ScrollView
android:id="@+id/scrollViewMonday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewmonday"
android:layout_above="@+id/linearLayoutmonday"
android:layout_alignParentLeft="true" >
<LinearLayout
android:id="@+id/MondayLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginTop="50dp"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
和xml new_line_entry
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/NewLineEntry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="17dp"
android:orientation="horizontal" >
<EditText
android:id="@+id/SubName"
android:layout_width="189dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="@string/subjectname"
android:paddingLeft="20dp" />
<Button
android:id="@+id/Time"
style="?android:attr/buttonbarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:layout_weight="1"
android:text="@string/time" />
<Button
android:id="@+id/Delete"
style="?android:attr/buttonbarButtonStyle"
android:layout_width="42dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_weight="1"
android:text="@string/crossdelete" />
</LinearLayout>
解决方法:
如果您希望不显示视图,则可以使其不可见或消失.您也可以从视图中删除它
使其不显示
view.setVisibility(View.GONE);
view.setVIsibility(View.VISIBLE);
真的删除它
parent = view.getParent();
parent.removeView(view);