今天来模拟一个注冊的界面过程:

点击“下一步”之后:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZW5zb24xNjg1NQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

说明一下:界面总局仅仅在一个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();
}
}
}

activity_regist.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: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方法什么的,仅仅要了解这个功能就可以,代码事实上和普通工具类似的。

android笔记5——同一个Activity中Fragment的切换的更多相关文章

  1. android小技巧:在activity中实现与绑定的fragment的回调

    看到标题你可能会想是一个多么高大上的技巧呢?事实上非常一般就是自己定义回调函数. 首先我们知道activity之间的数据传递有几种方式: 一是startActivityForResut()启动一个ac ...

  2. android开发之在activity中控制另一个activity的UI更新

    转自:http://blog.csdn.net/jason0539/article/details/18075293 第一种方法: 遇到一个问题,需要在一个activity中控制另一个acitivit ...

  3. Android开发常见的Activity中内存泄漏及解决办法

    上一篇文章楼主提到由Context引发的内存泄漏,在这一篇文章里,我们来谈谈Android开发中常见的Activity内存泄漏及解决办法.本文将会以“为什么”“怎么解决”的方式来介绍这几种内存泄漏. ...

  4. android 自定义控件View在Activity中使用findByViewId得到结果为null

    转载:http://blog.csdn.net/xiabing082/article/details/48781489 1.  大家常常自定义view,,然后在xml 中添加该view 组件..如果在 ...

  5. Android笔记---Intent实现Activity跳转

    学了之前的Android控件以及布局,我们就能够做一些UI的设计了,这里我结合之前的知识.以一个小的登录项目来解说下Activity之间跳转. 先看下效果图: 1.登录界面: 2.点击登录按钮跳转到另 ...

  6. Android(java)学习笔记169:Activity中的onCreate()方法分析

    1.onCreate( )方法是android应用程序中最常见的方法之一: 翻译过来就是说,onCreate()函数是在activity初始化的时候调用的,通常情况下,我们需要在onCreate()中 ...

  7. Android(java)学习笔记112:Activity中的onCreate()方法分析

    1.onCreate( )方法是android应用程序中最常见的方法之一: 翻译过来就是说,onCreate()函数是在activity初始化的时候调用的,通常情况下,我们需要在onCreate()中 ...

  8. android 在基类activity中注册BroadcastReceiver,子activity类实现响应

    android app 一般都会定义自己的BaseActivity, 如果各子Activity都需要接收广播但对广播的处理又不同时,可以考虑在BaseActivity中注册BroadcastRecei ...

  9. android开发学习——关于activity 和 fragment在toolbar上设置menu菜单

    在做一个项目,用的是Android Studio 系统的抽屉源码,但是随着页面的跳转,toolbar的title需要改变,toolbar上的menu菜单也需要改变,在网上找了好久,也尝试了很多,推荐给 ...

随机推荐

  1. JS(异步与单线程)

    JS(异步与单线程) 题目1.同步和异步的区别是什么,试举例(例子见知识点) 区别: 1.同步会阻塞代码执行,而异步不会 2.alert 是同步,setTimeout 是异步 题目2.关于 setTi ...

  2. go 和make的用法 区别

    Doand Make are two verbs which frequently confuse students of English. Learn the Difference between ...

  3. spring常用的注解

    一.使用注解之前要开启自动扫描功能,其中base-package为需要扫描的包(含子包). <context:component-scan base-package="cn.test& ...

  4. Axure:从单一评价方式到用户自由选择

    导读: 亲,还记得淘宝对货物的评价方式吗?还记得对快递哥的评价方式吗? 1,经典五星评:                                                         ...

  5. HDU-5423 Rikka with Tree。树深搜

    Rikka with Tree 题意:给出树的定义,给出树相似的定义和不同的定义,然后给出一棵树,求是否存在一颗树即和其相似又与其不同.存在输出NO,不存在输出YES. 思路:以1号节点为根节点,我们 ...

  6. 【Luogu】P3402最长公共子序列(LCS->nlognLIS)

    题目链接 SovietPower 的题解讲的很清楚.Map或Hash映射后用nlogn求出LIS.这里只给出代码. #include<cstdio> #include<cctype& ...

  7. SPOJ QTREE Query on a tree ——树链剖分 线段树

    [题目分析] 垃圾vjudge又挂了. 树链剖分裸题. 垃圾spoj,交了好几次,基本没改动却过了. [代码](自带常数,是别人的2倍左右) #include <cstdio> #incl ...

  8. bzoj1411: [ZJOI2009]硬币游戏

    1411: [ZJOI2009]硬币游戏 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 965  Solved: 420[Submit][Status ...

  9. 用 Jackson 来处理 JSON

    Jackson 是一个 Java 用来处理 JSON 格式数据的类库,性能非常好. 首先创建一个User对象类 (User.java) package com.sivalabs.json; impor ...

  10. 标准C程序设计七---31

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...