标签:android style class blog code java
今天来模拟一个注册的界面过程:
点击“下一步”之后:
说明一下:界面总局只在一个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>
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>
<?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>
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();
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方法什么的,只要了解这个功能即可,代码其实和普通工具类似的。
android笔记5——同一个Activity中Fragment的切换,布布扣,bubuko.com
android笔记5——同一个Activity中Fragment的切换
标签:android style class blog code java
原文地址:http://blog.csdn.net/enson16855/article/details/28393945