使用Application实现Fragment之间的传值
2015-09-24 21:15
232 查看
这里我们简单介绍一下Fragment的第三种传值方式:使用Application。
首先我们新建一个类继承于Application,然后类中的变量是我们要进行设置的传的变量。
然后在AndroidManifest.xml文件的<application选项中配置我们新建类。
也算是比较简单的一中传值方式吧。
下面的代码是我们今天的练习,就是通过在左边输入姓名然后点击改名按钮就可以把我们输入的姓名传到右边的Fragment中。
其实题目很简单,我们只是通过该题目来了解一下使用Application传值的步骤。
1、新建的类MyApp:
首先我们新建一个类继承于Application,然后类中的变量是我们要进行设置的传的变量。
然后在AndroidManifest.xml文件的<application选项中配置我们新建类。
也算是比较简单的一中传值方式吧。
下面的代码是我们今天的练习,就是通过在左边输入姓名然后点击改名按钮就可以把我们输入的姓名传到右边的Fragment中。
其实题目很简单,我们只是通过该题目来了解一下使用Application传值的步骤。
1、新建的类MyApp:
package org.mobiletrain.fragment_demo08; import android.app.Application; public class MyApp extends Application{ private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public MyApp(String name, int age) { super(); this.name = name; this.age = age; } public MyApp() { super(); // TODO Auto-generated constructor stub } }2、在清单文件中进行配置,就是第一句的name选项:
<application android:name="org.mobiletrain.fragment_demo08.MyApp" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >3、MainActivity实现传值:
package org.mobiletrain.fragment_demo08; import android.os.Bundle; import android.annotation.SuppressLint; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { FragmentManager manager; FragmentTransaction transaction; EditText nameEt; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); nameEt = (EditText)findViewById(R.id.nameEt); manager = getFragmentManager(); transaction = manager.beginTransaction(); transaction.add(R.id.rightLayout, new RightFragment()); transaction.commit(); } @SuppressLint("NewApi") public void change(View v){ String name = nameEt.getText().toString(); if (name!=null) { //使用getApplication()方法得到一个MyApp对象 MyApp myApp = (MyApp) getApplication(); //设置名字 myApp.setName(name); manager.beginTransaction(). replace(R.id.rightLayout, new RightFragment()).commit(); }else{ Toast.makeText(MainActivity.this, "请输入名字", 0).show(); } } }4、右边的Fragment接受MainActivity传来的值:
package org.mobiletrain.fragment_demo08; import android.annotation.SuppressLint; import android.app.Fragment; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; @SuppressLint("NewApi") public class RightFragment extends Fragment{ @SuppressLint("NewApi") @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.right, null); //使用当前Activity的getApplication()方法得到一个MyApp对象 MyApp myApp = (MyApp)getActivity().getApplication(); //得到传来的值 String name = myApp.getName(); TextView tv = (TextView)view.findViewById(R.id.rightTv); tv.setText(name); return view; } }下面就是截图了:
相关文章推荐
- 2016校招记
- cocos2d遍历所有body+判断触摸点是否在动态物体的形状内
- Android方法数不能超过65K的解决方案
- iOS多线程的一些使用心得
- cocos2d 坐标变换
- Android Api Demos登顶之路(八十)Graphics-->PathFillTypes
- Cocos2d-x 手机游戏《疯狂的蝌蚪》资源 “开源” win32+安德鲁斯+iOS三合一
- Android所遇到的问题归类
- 关于iOS国际化(根据手机App用户自己来切换语言) App支持多语言切换
- Android数据存储之SharedPreferences
- android 事件总线 -- Otto(四) HandlerFinder、DeadEvent
- 打造自己的chrome for Android
- Android基础学习之SharedPreferences
- 《ArcGIS Runtime SDK for Android开发笔记》——问题集:使用TextSymbol做标注显示乱码
- iOS 退出系统,返回到登陆界面
- IOS中的多线程
- android学习:Android布局、用控件
- iOS讲解迷惑深入浅出之基础动画-关键帧动画
- android权威编程指南学习笔记
- AndroidStudio(1 下载安装,环境搭建,使用设置)