我试图使用ViewPager
使用单页显示两个屏幕这是我的xml Part命名为 login :
<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到移动设备,验证otp应该在请求部分消失时进入下一个屏幕
答案 0 :(得分:1)
一旦你向设备发送OTP,你将收到一个回调,说明'成功',然后你的ViewPager.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();
}