2016-11-28 37 views
1

我想使用单页显示两个屏幕单页上我的XML部分命名为登录使用ViewPager</p> <p>这里显示两个屏幕使用ViewPager

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="0dip" 
    tools:context=".LoginActivity"> 

    <com.medmainfomatix.VoIPvoiceapp.MyViewPager 
     android:id="@+id/viewPagerVertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/layout_sms" 
     android:gravity="center_horizontal" 
     android:orientation="vertical"> 


     <android.support.design.widget.TextInputLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="60dp" 
      android:layout_gravity="center" 
      > 

      <EditText 
       android:id="@+id/loginNo" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:minWidth="200dp" 
       android:layout_gravity="center_horizontal" 
       android:digits="1234567890"> 

       <requestFocus /> 

      </EditText> 

     </android.support.design.widget.TextInputLayout> 

     <Button 
      android:id="@+id/btn_request_sms" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:visibility="gone" /> 


     <CheckBox 
      android:id="@+id/checkBox1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal"/> 

    </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/layout_otp" 
      android:gravity="center_horizontal" 
      android:orientation="vertical"> 

      <TextView 
       android:id="@+id/otptext" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal" 
       /> 
      <TextView 
       android:id="@+id/otptext1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal" 
      /> 

      <android.support.design.widget.TextInputLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       > 



      <EditText 
       android:id="@+id/inputOtp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       > 

       <requestFocus /> 

      </EditText> 

      </android.support.design.widget.TextInputLayout> 

      <Button 
       android:id="@+id/loginButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal" 
      /> 
     </LinearLayout> 
    </com.medmainfomatix.VoIPvoiceapp.MyViewPager> 
</RelativeLayout> 

这里是我的java活动文件命名为LoginActivity

public class abc extends BaseActivity implements SinchService.StartFailedListener,View.OnClickListener { 

    private static String TAG = LoginActivity.class.getSimpleName(); 

    private PrefManager pref; 
    private Button mLoginButton,btnRequestSms; 
    private EditText mLoginNo,inputOtp; 
    private ProgressDialog mSpinner; 
    private ViewPagerAdapter adapter; 
    private CheckBox mBox; 
    private SQLiteDatabase db; 
    private TextView otptext; 
    private ViewPager viewPager; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.login); 

     createDatabase(); 
     viewPager = (ViewPager) findViewById(R.id.viewPagerVertical); 
     mBox = (CheckBox) findViewById(R.id.checkBox1); 
     otptext= (TextView) findViewById(R.id.otptext); 
     inputOtp=(EditText) findViewById(R.id.inputOtp); 
     String checkBoxText = "I agree to all the <a href='http://www.redbus.in/mob/mTerms.aspx' > Terms and Conditions</a>"; 

     mBox.setText(Html.fromHtml(checkBoxText)); 
     pref = new PrefManager(this); 
     mBox.setMovementMethod(LinkMovementMethod.getInstance()); 
     btnRequestSms = (Button) findViewById(R.id.btn_request_sms); 
     mLoginButton = (Button) findViewById(R.id.loginButton); 
     mBox.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if(mBox.isChecked()){ 
        btnRequestSms.setVisibility(View.VISIBLE); 
       } 
       else{ 

        btnRequestSms.setVisibility(View.GONE); 
       } 
      } 
     }); 
     adapter = new ViewPagerAdapter(); 
     viewPager.setAdapter(adapter); 
     viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
      @Override 
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 
      } 

      @Override 
      public void onPageSelected(int position) { 
      } 

      @Override 
      public void onPageScrollStateChanged(int state) { 

      } 
     }); 

     btnRequestSms.setOnClickListener(this); 
     mLoginNo = (EditText) findViewById(R.id.loginNo); 
     mLoginButton.setEnabled(false); 
     mLoginButton.setOnClickListener(this); 

     // Checking for user session 
     // if user is already logged in, take him to main activity 
     if (pref.isLoggedIn()) { 
      Intent intent = new Intent(LoginActivity.this, PlaceCallActivity.class); 
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(intent); 

      finish(); 
     } 
     if (pref.isWaitingForSms()) { 
      viewPager.setCurrentItem(1); 

     } 

    } 

    public void onClick(View view) { 
     switch (view.getId()) { 
      case R.id.btn_request_sms: 
       validateForm(); 

       break; 

      case R.id.loginButton: 
       verifyOtp(); 
       loginClicked(); 
       break; 

     } 
    } 

    private void loginClicked() { 
     String userName = mLoginNo.getText().toString(); 

     if (userName.isEmpty()) { 
      Toast.makeText(this, "Please enter your number", Toast.LENGTH_LONG).show(); 
      return; 
     } 


    } 

    private void openPlaceCallActivity() { 
     Intent mainActivity = new Intent(this, abc2.class); 
     startActivity(mainActivity); 
    } 
private void requestForSMS(final String mobile) { 
    StringRequest strReq = new StringRequest(Request.Method.POST, 
      Config.URL_REQUEST_SMS, new Response.Listener<String>() { 

     @Override 
     public void onResponse(String response) { 
      Log.d(TAG, response.toString()); 

      try { 
       JSONObject responseObj = new JSONObject(response); 

       // Parsing json object response 
       // response will be a json object 
       boolean error = responseObj.getBoolean("error"); 
       String message = responseObj.getString("message"); 

       // checking for error, if not error SMS is initiated 
       // device should receive it shortly 
       if (!error) { 
        // boolean flag saying device is waiting for sms 
        pref.setIsWaitingForSms(true); 
        viewPager.setCurrentItem(1); 
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show(); 

       } else { 
        Toast.makeText(getApplicationContext(), 
          "Error: " + message, 
          Toast.LENGTH_LONG).show(); 
       } 

       // hiding the progress bar 


      } catch (JSONException e) { 
       Toast.makeText(getApplicationContext(), 
         "Error: " + e.getMessage(), 
         Toast.LENGTH_LONG).show(); 


      } 

     } 
    }, new Response.ErrorListener() { 

     @Override 
     public void onErrorResponse(VolleyError error) { 
      Log.e(TAG, "Error: " + error.getMessage()); 
      Toast.makeText(getApplicationContext(), 
        error.getMessage(), Toast.LENGTH_SHORT).show(); 
     } 
    }) { 

     /** 
     * Passing user parameters to our server 
     * @return 
     */ 
     @Override 
     protected Map<String, String> getParams() { 
      Map<String, String> params = new HashMap<String, String>(); 
      params.put("mobile", mobile); 

      Log.e(TAG, "Posting params: " + params.toString()); 

      return params; 
     } 

    }; 

    // Adding request to request queue 
    MyApplication.getInstance().addToRequestQueue(strReq); 
} 

    class ViewPagerAdapter extends PagerAdapter { 

     @Override 
     public int getCount() { 
      return 2; 
     } 

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

     public Object instantiateItem(View collection, int position) { 

      int resId = 0; 
      switch (position) { 
       case 0: 
        resId = R.id.layout_sms; 
        break; 
       case 1: 
        resId = R.id.layout_otp; 
        break; 
      } 
      return findViewById(resId); 
     } 
    } 
} 

这里是我MyViewPager类:

public class MyViewPager extends ViewPager { 

    public MyViewPager(Context context) { 
     super(context); 
    } 

    public MyViewPager(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent event) { 
     // Never allow swiping to switch between pages 
     return false; 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     // Never allow swiping to switch between pages 
     return false; 
    } 
} 

如果你没有得到任何东西,请发表评论,因为我已经砍了我的大部分代码,也许错误地让我误板缺有关MyViewPager东西,所以请随时发表评论,这样我可以编辑我的代码,你可以得到更清晰的概念否则,如果你已经得到,那么你能明白我想在这里实现

目标:要请求OTP移动和验证检察官办公室应来,同时要求下一屏零件不见了

+0

你不能真正得到“两页”,但你可以看到“下一个和以前”的。 http://stackoverflow.com/questions/9907748/viewpager-get-a-partial-view-of-the-next-page –

+0

一旦你发送一个OTP到设备,你会收到一个回调,说'成功',然后yourViewPager。 setCurrentItem(1);这将移动到下一个屏幕。 –

+0

@RushiAyyappa好的,这是什么setCurrentItem正在做什么,我有点困惑,以及如果我设置0而不是1 – SameerKhan1406

回答

1

一旦你发送一个OTP到设备,你将收到一个回调,说明'成功',然后yourViewPager.setCurrentItem(1);这将移动到下一个屏幕

if (!error) { 
        // boolean flag saying device is waiting for sms 
        pref.setIsWaitingForSms(true); 
        viewPager.setCurrentItem(1);//this will move to next screen. 
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show(); 

       } else { 
        Toast.makeText(getApplicationContext(), 
          "Error: " + message, 
          Toast.LENGTH_LONG).show(); 
       } 
+0

伟大的工作。非常感谢 – SameerKhan1406

相关问题