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菜单也需要改变,在网上找了好久,也尝试了很多,推荐给 ...
随机推荐
- Ubuntu 16.04 LTS上git提交出现警告Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts. 的解决方法
问题: Ubuntu 16.04 LTS执行 git pull时总会出现以下警告: Warning: Permanently added 'github.com,52.74.223.119' (RSA ...
- ZOJ 3469 区间DP Food Delivery
题解 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm ...
- Flash生成HTML5动画方法
方法一:利用“swiffy”将Flash转换成HTML5动画. 首先,我们需要下载一款基于“Flash”程序的插件,名称为“swiffy”,这是一款由谷歌推出的一个Flash扩展,可以通过“Fla ...
- js request学习
SP的内置对象在JSP页面中无须声明就可以直接使用,其内置对象常用的有Request,response,session,application,out,config,pageCOntext reque ...
- Knockout v3.4.0 中文版教程-6-计算监控-可写的计算监控
2.可写的计算监控 初学者可能想要跳过本节 - 可写的计算监控是相当高级的部分,在大多数情况下不是必需的. 通常,计算监控是一个通过其他监控值计算出的值,因此是只读的. 令人惊讶的是,可以使计算监控值 ...
- luogu3980 [NOI2008]志愿者招募
神题,还不太清楚 #include <iostream> #include <cstring> #include <cstdio> #include <que ...
- angular controller与directive相互引用
/** * Created by Administrator on 2017/8/28. */ var app =angular.module('app',[]); app.directive('fo ...
- HDU-1392 Surround the Trees,凸包入门!
Surround the Trees 此题讨论区里大喊有坑,原谅我没有仔细读题还跳过了坑点. 题意:平面上有n棵树,选一些树用绳子围成一个包围圈,使得所有的树都在这个圈内. 思路:简单凸包入门题,凸包 ...
- 九度oj 题目1016:火星A+B
题目描述: 读入两个不超过25位的火星正整数A和B,计算A+B.需要注意的是:在火星上,整数不是单一进制的,第n位的进制就是第n个素数.例如:地球上的10进制数2,在火星上记为“1,0”,因为 ...
- sqlserver查询表大小
IF OBJECT_ID('tempdb..#TB_TEMP_SPACE') IS NOT NULL DROP TABLE #TB_TEMP_SPACE GO CREATE TABLE #TB_TEM ...