android笔记5——同一个Activity中Fragment的切换
今天来模拟一个注冊的界面过程:
点击“下一步”之后:
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的切换的更多相关文章
- android小技巧:在activity中实现与绑定的fragment的回调
看到标题你可能会想是一个多么高大上的技巧呢?事实上非常一般就是自己定义回调函数. 首先我们知道activity之间的数据传递有几种方式: 一是startActivityForResut()启动一个ac ...
- android开发之在activity中控制另一个activity的UI更新
转自:http://blog.csdn.net/jason0539/article/details/18075293 第一种方法: 遇到一个问题,需要在一个activity中控制另一个acitivit ...
- Android开发常见的Activity中内存泄漏及解决办法
上一篇文章楼主提到由Context引发的内存泄漏,在这一篇文章里,我们来谈谈Android开发中常见的Activity内存泄漏及解决办法.本文将会以“为什么”“怎么解决”的方式来介绍这几种内存泄漏. ...
- android 自定义控件View在Activity中使用findByViewId得到结果为null
转载:http://blog.csdn.net/xiabing082/article/details/48781489 1. 大家常常自定义view,,然后在xml 中添加该view 组件..如果在 ...
- Android笔记---Intent实现Activity跳转
学了之前的Android控件以及布局,我们就能够做一些UI的设计了,这里我结合之前的知识.以一个小的登录项目来解说下Activity之间跳转. 先看下效果图: 1.登录界面: 2.点击登录按钮跳转到另 ...
- Android(java)学习笔记169:Activity中的onCreate()方法分析
1.onCreate( )方法是android应用程序中最常见的方法之一: 翻译过来就是说,onCreate()函数是在activity初始化的时候调用的,通常情况下,我们需要在onCreate()中 ...
- Android(java)学习笔记112:Activity中的onCreate()方法分析
1.onCreate( )方法是android应用程序中最常见的方法之一: 翻译过来就是说,onCreate()函数是在activity初始化的时候调用的,通常情况下,我们需要在onCreate()中 ...
- android 在基类activity中注册BroadcastReceiver,子activity类实现响应
android app 一般都会定义自己的BaseActivity, 如果各子Activity都需要接收广播但对广播的处理又不同时,可以考虑在BaseActivity中注册BroadcastRecei ...
- android开发学习——关于activity 和 fragment在toolbar上设置menu菜单
在做一个项目,用的是Android Studio 系统的抽屉源码,但是随着页面的跳转,toolbar的title需要改变,toolbar上的menu菜单也需要改变,在网上找了好久,也尝试了很多,推荐给 ...
随机推荐
- POJ 2486 树形背包DP Apple Tree
设d(u, j, 0)表示在以u为根的子树中至多走k步并且最终返回u,能吃到的最多的苹果. 则有状态转移方程: #include <iostream> #include <cstdi ...
- localstorage与sessionstorage的使用
cookie,sessionStorage,localeStorage的区别 cookie是存储在浏览器端,并且随浏览器的请求一起发送到服务器端的,它有一定的过期时间,到了过期时间自动会消失.sess ...
- Configure Red Hat Enterprise Linux shared disk cluster for SQL Server
下面一步一步介绍一下如何在Red Hat Enterprise Linux系统上为SQL Server配置共享磁盘集群(Shared Disk Cluster)及其相关使用(仅供测试学习之用,基础篇) ...
- strcat strcpy 源代码,用指针去实现
15. 指针实现 strcmp函数 • int newStrcpy(char *p, char *q) • { • while(*p ==*q) { ...
- oracle 安装 启动listener 建库相关
安装 参考 http://www.cnblogs.com/gaojun/archive/2012/11/22/2783257.html 几个问题: 1. 用户删除问题 p001:~ # userdel ...
- docker.io/centos安装phpstudy
1.准备工作 yum install vimyum install wget yum install make yum -y install bzip2 yum install m4 yum inst ...
- python3--__repr_和__str__会返回字符串表达形式
__repr_和__str__会返回字符串表达形式 下一个例子是已经见过的init构造方法和add重载方法,本例也会定义返回实例的字符串表达形式的__repr__方法.字符串格式把self.data对 ...
- 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)
先把代码扔上来 E. Field of Wonders time limit per test 3 seconds memory limit per test 256 megabytes input ...
- Unity3D for iOS初级教程:Part 2/3
转自Unity3D for iOS 这篇文章还可以在这里找到 英语 Learn how to use Unity to make a simple 3D iOS game! 这篇教材是来自教程团队成员 ...
- 阐述struts2的执行流程。
Struts 2框架本身大致可以分为3个部分:核心控制器FilterDispatcher.业务控制器Action和用户实现的企业业务逻辑组件. 核心控制器FilterDispatcher是Struts ...