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

点击“下一步”之后:

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. python基础——11(模块初识)

    一.函数回调 # 提前写出函数的调用,再去考虑函数体的实现 # 怎么样提前写出函数的调用:在另一个函数中写出函数的调用 # 再去考虑函数体的实现:根据实际的需求 # 自定义一个sleep def my ...

  2. 序列化 random模块应用

    序列化 我们今天学习下序列化,什么是序列化呢? 将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. 为什么要有序列化模块: 比如,我们在python代码中计算的一个数据需要给另外一段程序使用 ...

  3. jsonp实现跨域访问json数据

    前台js function init() { $.ajax({ url: 'http://localhost:8012/index.json', dataType: "jsonp" ...

  4. ospf 提升 二 ---LSA

    ospf ABR和ASBR的区别 官方建议中大型网络的规模参考   根据spf算法   而不是路由器的硬件性能强弱 a ABR最多关联3个区域 b 单区域内路由器最多50台 c 一台运行ospf的路由 ...

  5. NYOJ 293 Sticks

    Sticks 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 George took sticks of the same length and cut them r ...

  6. lamp环境配置,ubunutu下

    Ubuntu下快速搭建LAMP环境过程记录: 安装 Apache2: sudo apt-get install apache2 安装PHP模块: sudo apt-get install php5 安 ...

  7. HDU——2612Find a way(多起点多终点BFS)

    Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. SPOJ NSUBSTR Substrings ——后缀自动机

    建后缀自动机 然后统计次数,只需要算出right集合的大小即可, 然后更新f[l[i]]和rit[i]取个max 然后根据rit集合短的一定包含长的的性质,从后往前更新一遍即可 #include &l ...

  9. c++ primer note

    ---恢复内容开始--- 1.decltype 2.auto 3.cbegin 4.cend 5.constexpr 6.(*Parray)[10]=&arr; //Parray 指向一个含有 ...

  10. *AtCoder Regular Contest 096F - Sweet Alchemy

    $n \leq 50$的树,每个点有权值,现要选点(可多次选一个点)使点数尽量多,如下限制:选的总权值不超过$C \leq 1e9$:$c_i$表示$i$选的次数,$p_i$表示$i$的父亲,那么$c ...