android笔记5——同一个Activity中Fragment的切换
今天来模拟一个注冊的界面过程:
点击“下一步”之后:
说明一下:界面总局仅仅在一个Activity里面。
1、首先定义RegistActivity
public class RegistActivity extends Activity { private EditText userEditText; private EditText verifyCodeText; private Fragment verifyCodeFragment; private Fragment checkCodeFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_regist); if (savedInstanceState == null) { verifyCodeFragment = new VerifyCodeFragment(); getFragmentManager().beginTransaction() .add(R.id.activity_regist, verifyCodeFragment).commit(); } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_regist" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.javen.activity.RegistActivity" > </LinearLayout>
这边通过java代码来增加Fragment。
2、fragment_verifycode.xml 获取验证码的界面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" tools:context="com.javen.activity.fragment.VerifyCodeFragment" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/message" android:textSize="@dimen/label_font_size" /> <EditText android:id="@+id/userEditText" android:layout_width="200dp" android:layout_height="wrap_content" android:inputType="text" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" android:textSize="@dimen/label_font_size" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" > <Button android:id="@+id/bnRegist" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:onClick="verifyCodeListener" android:text="@string/next" /> </LinearLayout> </LinearLayout>
2、输入验证码的界面:fragment_checkcode.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" tools:context="com.javen.activity.fragment.CheckCodeFragment" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/verifyCode" android:textSize="@dimen/label_font_size" /> <EditText android:id="@+id/verifyCodeText" android:layout_width="200dp" android:layout_height="wrap_content" android:inputType="text" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" > <Button android:id="@+id/bnRegist" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:onClick="checkCodeListener" android:text="@string/next" /> </LinearLayout> </LinearLayout>
3、设置buttonListener,此处须要在Activity里面加入方法。
verifyCodeListener:
public void verifyCodeListener(View source) { userEditText = (EditText) verifyCodeFragment.getView().findViewById( R.id.userEditText); String phoneNumber = userEditText.getText().toString().trim(); if (!Tools.matchPhone(phoneNumber)) {<span style="white-space:pre"> </span>//对手机号码验证的一个正則表達式方法 DialogUtil.showDialog(this, Constant.LOGIN_USER_NAME, false); return; } this.getVerifyCode(phoneNumber);<span style="white-space:pre"> </span>//Http请求获取验证码 // 释放当前的fragment。又一次设置短信验证码输入的fragment FragmentTransaction transaction = getFragmentManager() .beginTransaction(); transaction.remove(verifyCodeFragment); checkCodeFragment = new CheckCodeFragment(); transaction.add(R.id.activity_regist, checkCodeFragment).commit(); }主要切换代码为:
<span style="white-space:pre"> </span>// 释放当前的fragment。又一次设置短信验证码输入的fragment FragmentTransaction transaction = getFragmentManager() .beginTransaction(); transaction.remove(verifyCodeFragment); checkCodeFragment = new CheckCodeFragment(); transaction.add(R.id.activity_regist, checkCodeFragment).commit();
checkCodeListener:
public void checkCodeListener(View source) { verifyCodeText = (EditText) checkCodeFragment.getView().findViewById( R.id.verifyCodeText); String verifyCode = verifyCodeText.getText().toString().trim(); Map<String, String> params = new HashMap<String, String>(); params.put("verifyCode", verifyCode); String url = UrlsUtil.formatUrl(UrlConstant.REGIST_CHECKCODE); String result = null; try { result = HttpsUtil.postRequest(url, params); } catch (Exception e) { e.printStackTrace(); DialogUtil.showDialog(this, Constant.SERVICE_ERRO, false); } if(result != null){ //TODO } }
说明一下:
这边主要说明的是Fragment切换的过程,至于Http请求,Util方法什么的,仅仅要了解这个功能就可以,代码事实上和普通工具类似的。