我开发了一个项目,Tablayout
项目包含3个viewpager
个项目,从edittext
viewpager
获取数据,我需要清除{{1}中的所有数据当发生在最后viewpager
项中设置的按钮点击事件时,将所有我们设置为空。
这是父viewpager
,fragment
。class
Homefragment
Customer.class
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = inflater.inflate(R.layout.activity_main, container, false);
super.onCreate(savedInstanceState);
ButterKnife.bind(this, view);
ButterKnife.bind(this, view);
final ViewPager viewPager = ButterKnife.findById(view, R.id.viewpager2);
setupViewPager(viewPager);
TabLayout tabLayout = ButterKnife.findById(view, R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
next = ButterKnife.findById(view, R.id.nxt_btn);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
}
});
return view;
}
private void setupViewPager(final ViewPager viewPager) {
final ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new Customer_details(), "Customer Details");
adapter.addFragment(new Order_details(), "Order Details");
adapter.addFragment(new Payment_details(), "Payment Details");
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (viewPager.getCurrentItem() == 2) {
next.setVisibility(View.INVISIBLE);
} else if (viewPager.getCurrentItem() == 0) {
next.setVisibility(View.VISIBLE);
} else if (viewPager.getCurrentItem() == 1) {
next.setVisibility(View.VISIBLE);
}
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); ++i) {
View innerview = ((ViewGroup) view).getChildAt(i);
setupUI(innerview);
}
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
//unbinder.unbind();
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public void onButtonPressed() {
startActivity(new Intent(null, MainActivity.class));
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP
&& keyCode == KeyEvent.KEYCODE_BACK) {
// handle back button
Intent i=new Intent(getActivity(),MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
getActivity().finish();
return true;
}
return false;
}
});
}
}
Order.class
public class Customer_details extends Fragment {
public static int selectedid;
public static EditText cname, cnumber, cnumber2, caddress, cpincode, clandmark;
public static Spinner spinner, snum, snum1, sp1, sp2;
public static RadioGroup radioGroup;
public static RadioButton radioButton;
TextView ctype, name, contact1, contact2, dispatch, address, pincode, landmark, source;
private PayOrder_Interface pay;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//return
final View view = inflater.inflate(R.layout.activity_cdetails, container, false);
cname = (EditText) view.findViewById(R.id.cname);
cnumber = (EditText) view.findViewById(R.id.cnumber1);
cnumber2 = (EditText) view.findViewById(R.id.cnumber2);
caddress = (EditText) view.findViewById(R.id.caddress);
cpincode = (EditText) view.findViewById(R.id.cpincode);
clandmark = (EditText) view.findViewById(R.id.clandmark);
String[] values = {"Select", "Retail", "Bulk"};
spinner = (Spinner) view.findViewById(R.id.spinner);
final String[] value = {"Select", "+91", "+64", "+355", "+213", "+214", "+54", "+374", "+43", "+994", "+1", "+973", "+880", "+32", "+1-(441)", "+591", "+387", "+267", "+55", "359", "+673"};
snum = (Spinner) view.findViewById(R.id.spinner3);
String[] val = {"Select", "+91", "+64", "+355", "+213", "+214", "+54", "+374", "+43", "+994", "+1", "+973", "+880", "+32", "+1-(441)", "+591", "+387", "+267", "+55", "359", "+673"};
snum1 = (Spinner) view.findViewById(R.id.spinner4);
String[] v = {"Select", "Online", "Existing", "Sales Team", "Store", "Zomato"};
sp1 = (Spinner) view.findViewById(R.id.spinner_2);
String[] b = {"Select", "GaarG Store 1", "PG Tech"};
sp2 = (Spinner) view.findViewById(R.id.spinner_bulk);
radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
switch (checkedId) {
case R.id.radioButton1:
// switch to fragment 1
break;
case R.id.radioButton2:
// Fragment 2
break;
}
selectedid = radioGroup.getCheckedRadioButtonId();
radioButton = (RadioButton) view.findViewById(selectedid);
}
});
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values);
ArrayAdapter<String> a = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, val);
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, value);
ArrayAdapter<String> adap = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, v);
ArrayAdapter<String> bulk = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, b);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
adapt.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
a.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
adap.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
bulk.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(adapter);
snum1.setAdapter(a);
snum.setAdapter(adapt);
sp1.setAdapter(adap);
sp2.setAdapter(bulk);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
// String txt=(String)spinner.getSelectedItem();
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Object item = parentView.getItemAtPosition(position);
/* if(item!=null){
Toast.makeText(getContext(),item.toString() + "Selected",Toast.LENGTH_SHORT).show();
}*/
//EventBus.getDefault().post(value);
switch (parentView.getSelectedItemPosition()) {
case (0): {
sp2.setVisibility(View.GONE);
//Do Nothing
}
break;
case (1): {
contact1.setVisibility(View.VISIBLE);
contact2.setVisibility(View.VISIBLE);
dispatch.setVisibility(View.VISIBLE);
address.setVisibility(View.VISIBLE);
pincode.setVisibility(View.VISIBLE);
landmark.setVisibility(View.VISIBLE);
source.setVisibility(View.VISIBLE);
cname.setVisibility(View.VISIBLE);
cnumber.setVisibility(View.VISIBLE);
cnumber2.setVisibility(View.VISIBLE);
caddress.setVisibility(View.VISIBLE);
cpincode.setVisibility(View.VISIBLE);
ctype.setVisibility(View.VISIBLE);
clandmark.setVisibility(View.VISIBLE);
spinner.setVisibility(View.VISIBLE);
sp1.setVisibility(View.VISIBLE);
snum.setVisibility(View.VISIBLE);
snum1.setVisibility(View.VISIBLE);
sp2.setVisibility(View.GONE);
radioGroup.setVisibility(View.VISIBLE);
// radioButton_1.setVisibility(View.VISIBLE);
//radioButton_2.setVisibility(View.VISIBLE);
}
break;
case (2): {
contact1.setVisibility(View.GONE);
contact2.setVisibility(View.GONE);
dispatch.setVisibility(View.GONE);
address.setVisibility(View.GONE);
pincode.setVisibility(View.GONE);
landmark.setVisibility(View.GONE);
source.setVisibility(View.GONE);
cname.setVisibility(View.GONE);
cnumber.setVisibility(View.GONE);
cnumber2.setVisibility(View.GONE);
caddress.setVisibility(View.GONE);
cpincode.setVisibility(View.GONE);
clandmark.setVisibility(View.GONE);
ctype.setVisibility(View.VISIBLE);
spinner.setVisibility(View.VISIBLE);
snum.setVisibility(View.GONE);
snum1.setVisibility(View.GONE);
sp1.setVisibility(View.GONE);
sp2.setVisibility(View.VISIBLE);
radioGroup.setVisibility(View.GONE);
// radioButton_1.setVisibility(View.GONE);
// radioButton_2.setVisibility(View.GONE);
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return view;
}
public String getEditetxtValue() {
return cname.getText().toString();
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP
&& keyCode == KeyEvent.KEYCODE_BACK) {
// handle back button
Intent a = new Intent(getActivity(), MainActivity.class);
startActivity(a);
getActivity().finish();
return true;
}
return false;
}
});
}
Payment.class
public class Order_details extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
private static final String[] FLAVOURS = new String[]{"Venilla", "Stawberry", "Chocalate", "Mango", "Pineapple"};
public static EditText edittext, edmessage;
public static Spinner spinner, sp, select;
public static AutoCompleteTextView textView;
DatePickerDialog.OnDateSetListener ondate = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
edittext.setText(String.valueOf(dayOfMonth) + "-" + String.valueOf(monthOfYear + 1)
+ "-" + String.valueOf(year));
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate (R.layout.activity_odetails, container, false);
edmessage = (EditText) view.findViewById(R.id.ed_cake);
String[] egg = {"Select", "Egg", "Eggless", "N/A"};
spinner = (Spinner) view.findViewById(R.id.spinner_e);
String[] weight = {"Select", "0.5kg", "1kg", "1.5kg", "2kg", "2.5kg", "3kg", "6kg", "5kg", "4kg", "4.5kg", "3.5kg", "7kg", "8kg,", "7.5kg", "10kg"};
sp = (Spinner) view.findViewById(R.id.spinner_wt);
String[] time = {"Select", "8Am- 9Am", "11.30pm-11.45pm", "9am-10am", "10am-11am", "11AM=12PM", "12PM-1PM", "1PM-2PM", "2PM-3PM", "3PM-4PM", "4PM-5PM", "5PM-6PM", "6PM-7PM", "7PM-8PM", "8PM-9PM"};
select = (Spinner) view.findViewById(R.id.spinner_t);
textView = (AutoCompleteTextView) view.findViewById(R.id.au_flavour);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, egg);
ArrayAdapter<String> p = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, weight);
ArrayAdapter<String> d = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, time);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
ArrayAdapter<String> au = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_dropdown_item_1line, FLAVOURS);
p.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
d.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(adapter);
sp.setAdapter(p);
textView.setAdapter(au);
select.setAdapter(d);
getActivity().getWindow().setSoftInputMode(WindowManager.
LayoutParams.SOFT_INPUT_ADJUST_PAN);
return view;
}
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
edittext = (EditText) view.findViewById(R.id.ed_date);
edittext.setInputType(InputType.TYPE_NULL);
edittext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDatePicker();
}
});
}
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
activity.getCurrentFocus().getWindowToken(), 0);
}
public void setupUI(View view) {
if (!(view instanceof EditText)) {
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard(getActivity());
return false;
}
});
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); ++i) {
View innerview = ((ViewGroup) view).getChildAt(i);
setupUI(innerview);
}
}
}
private void showDatePicker() {
Bundle bundle=new Bundle();
Calendar calender = Calendar.getInstance();
Bundle args = new Bundle();
args.putInt("year", calender.get(Calendar.YEAR));
args.putInt("month", calender.get(Calendar.MONTH));
args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
date.setArguments(args);
date.setCallBack(ondate);
date.show(getFragmentManager(), "Date Picker");
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP
&& keyCode == KeyEvent.KEYCODE_BACK) {
startActivity(new Intent(getActivity(), MainActivity.class));
getActivity().finish();
return true;
}
return false;
}
});
}
我试图通过这种方法清除项目,
public class Payment_details extends Fragment {
public static TextView payment, advance, balance, discount, extra, extra_amount, delivery;
public static EditText e_amount, e_advance, e_balance, e_discount, e_extra, e_extra_amount, e_delivery_charges, e_remark;
public static Spinner spinner;
Context context;
Button addextra, reset, save;
private LinearLayout laytype, layamount, laybalance, laydiscount, layexamt, laydelivery, layextra;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_payment_details, container, false);
e_amount = (EditText) view.findViewById(R.id.edtAmount);
e_advance = (EditText) view.findViewById(R.id.edtAdvance);
e_balance = (EditText) view.findViewById(R.id.edtBalance);
e_discount = (EditText) view.findViewById(R.id.e_discount);
e_extra = (EditText) view.findViewById(R.id.e_extra);
e_remark = (EditText) view.findViewById(R.id.e_remarks);
e_extra_amount = (EditText) view.findViewById(R.id.eextra_amount);
e_delivery_charges = (EditText) view.findViewById(R.id.e_delivery);
addextra = (Button) view.findViewById(R.id.add_extra);
reset = (Button) view.findViewById(R.id.btn_reset);
save = (Button) view.findViewById(R.id.save2);
String[] ptype = {"Select", "Cash", "EDC", "Online", "NEFT", "Cheque", "Not Paid"};
spinner = (Spinner) view.findViewById(R.id.spinner_pt);
laydiscount.setVisibility(View.GONE);
layextra.setVisibility(View.GONE);
layexamt.setVisibility(View.GONE);
laydelivery.setVisibility(View.GONE);
if (Customer_details.spinner.getSelectedItem().toString().equalsIgnoreCase("Bulk")) {
payment.setVisibility(View.GONE);
advance.setVisibility(View.GONE);
balance.setVisibility(View.GONE);
e_advance.setVisibility(View.GONE);
e_balance.setVisibility(View.GONE);
spinner.setVisibility(View.GONE);
}
textWatcher(e_advance);
textWatcher(e_discount);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, ptype);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(adapter);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
addextra.setOnClickListener(new View.OnClickListener() {
int click = 0;
//int i=0;
@Override
public void onClick(View v) {
// int count =++_clicks;
if (click == 0) {
click = 1;
laydiscount.setVisibility(View.VISIBLE);
layextra.setVisibility(View.VISIBLE);
layexamt.setVisibility(View.VISIBLE);
laydelivery.setVisibility(View.VISIBLE);
} else {
click = 0;
laydiscount.setVisibility(View.GONE);
layextra.setVisibility(View.GONE);
layexamt.setVisibility(View.GONE);
laydelivery.setVisibility(View.GONE);
}
}
});
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
clear();
Toast.makeText(getActivity(), "Working", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
private void textWatcher(final EditText edit) {
edit.addTextChangedListener(new TextWatcher() {
int result,r=0;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
result = 0;
//r=0;
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int e = parse(e_amount) - parse(e_advance);
if (edit == e_discount && parse(e_discount) == 0) {
result = e;
}
else{
result = e - parse(e_discount);
}
}
@Override
public void afterTextChanged(Editable s) {
e_balance.setText(String.valueOf(result));
}
});
}
private int parse(EditText edt) {
String s = edt.getText().toString();
if (s.matches(""))s = "0";
return Integer.parseInt(s);
}
public void clear() {
Customer_details.spinner.setSelection(0);
Log.v(Customer_details.cname.getText().toString(), "name");
try {
Customer_details.cname.setText("");
Customer_details.snum.setSelection(0);
Customer_details.cnumber.setText("");
Customer_details.snum1.setSelection(0);
Customer_details.sp2.setSelection(0);
Customer_details.cnumber2.getText().clear();
Customer_details.caddress.getText().clear();
Customer_details.cpincode.getText().clear();
Customer_details.clandmark.getText().clear();
// Customer_details.radioButton.setChecked(false);
Customer_details.sp1.setSelection(0);
Customer_details.spinner.setSelection(0);
Customer_details.cnumber.setText("");
Order_details.spinner.setSelection(0);
Order_details.textView.setText("");
Order_details.sp.setSelection(0);
Order_details.edmessage.setText("");
Order_details.edittext.getText().clear();
Order_details.select.setSelection(0);
e_amount.setText("");
spinner.setSelection(0);
e_advance.setText("");
e_balance.setText("");
e_remark.setText("");
e_discount.setText("");
e_extra.setText("");
e_extra_amount.setText("");
e_delivery_charges.setText("");
spinner.setSelection(0);
e_amount.setText("");
e_advance.setText("");
e_balance.setText("");
e_remark.setText("");
} catch (Exception e) {
Log.i("MYLOG", "Message was not handled " + e.getMessage());
}
Toast.makeText(getActivity(), "Cleared ", Toast.LENGTH_SHORT).show();
}
}
edittext.setText(" ");
仅清除订单和付款类数据客户数据无法清除。如何解决我的问题?
答案 0 :(得分:1)
尝试在mViewPager.setOffscreenPageLimit(3);
方法
setUpViewPager()
<强> Upate 强>
默认情况下,setOffscreenPageLimit()
将设置为1,这意味着您将同时激活2个片段页面。由于您需要清除所有三个片段页面EditText
中的文本,然后设置setOffscreenPageLimit(3)
(因为您使用的是3个片段)将使所有3个片段页面处于活动状态并有助于对所有片段进行必要的更改他们有关详细信息,请参阅Docs
void setOffscreenPageLimit(int limit) 在空闲状态下设置应保留到视图层次结构中当前页面任一侧的页数。超出此限制的页面将在需要时从适配器重新创建。