如何在所有页面的edittext的最后一页的页面查看器中验证所有屏幕?

时间:2019-04-08 06:46:26

标签: android android-viewpager

我想将验证设置为最后一个屏幕,即所有滑动屏幕数据将由用户插入或不使用viewpager。

pageradapter类

public class MyViewPagerAdapter extends PagerAdapter {
    private LayoutInflater layoutInflater;
    private Context context;
    private int[] layouts;

    public MyViewPagerAdapter(Context context,int[] layouts) {
        this.context=context;
        this.layouts=layouts;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View view = layoutInflater.inflate(layouts[position], container, false);
        container.addView(view);

        return view;
    }

    @Override
    public int getCount() {
        return layouts.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object obj) {
        return view == obj;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view = (View) object;
        container.removeView(view);
    }

在活动oncreate方法中将布局添加到viewpager

layouts = new int[]{
                R.layout.welcome_side1,
                R.layout.welcome_side2,
                R.layout.welcome_side3,
                R.layout.welcome_side4};

        // making notification bar transparent
        changeStatusBarColor();

        myViewPagerAdapter = new MyViewPagerAdapter(context,layouts);
        viewPager.setAdapter(myViewPagerAdapter);
        viewPager.addOnPageChangeListener(viewPagerPageChangeListener);

在我要在此处验证的第一个屏幕编辑文本上单击按钮的活动

btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int current = getItem(+1);
                if (current < layouts.length) {
                    // move to next screen
                    viewPager.setCurrentItem(current);
                } else {
                    //i want to validate here about my all layout files empty fields or user insert correct valuers.
                }
            }
        });

我所有的布局屏幕都像我想验证编辑文本是否有价值,然后使用页面适配器跳转到特定屏幕。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bg_screen1">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linearlayout1"
        android:layout_marginBottom="40dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name" />
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

使用MVP模式或至少使Presenter零件/某物用于数据存储(每个片段分开),然后稍后从它们中获取数据。如果不确定,该怎么做,然后问自己如何处理屏幕方向(也许您已经有此用途,因此请尝试使用它)(不要说onSaveInstanceState())。

答案 1 :(得分:0)

尝试这样做

btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int current = getItem(+1);
            if (current < layouts.length) {
                // move to next screen
                viewPager.setCurrentItem(current);
            } else {
                for (int i = 0; i < viewPager.getChildCount(); i++) {
                   View v = viewPager.getChildAt(i);
                   EditText ed = v.findViewById(R.id.editText);
                   if (ed != null) {
                       if (ed.getText() == null || ed.getText().toString().equals("")) {
                             // here text is empty, do something...
                           return;
                       }
                    }
                }
                // here, all edittext were edited, do something
            }
        }
    });