关闭

Activity向Fragment中传值

标签: AndroidFragment
186人阅读 评论(0) 收藏 举报
分类:

说明:

activity向fragment中传值   

1。要传的值  放到Bundle对象里
2。通过创建传递封Fragment 将bundle对象 通过 setArguments()传递到fragment

3.Fragment中 通过getArguments() 得到 bundle对象  就能得到里面的值

代码:

MainActivity:

    package com.qianfeng.activitytofragment;  
      
    import android.annotation.SuppressLint;  
    import android.app.Activity;  
    import android.app.FragmentManager;  
    import android.app.FragmentTransaction;  
    import android.os.Bundle;  
    import android.view.View;  
    import android.widget.EditText;  
      
    @SuppressLint("NewApi")   
    public class MainActivity extends Activity {  
      
        private EditText et1;  
        private FragmentManager manager;  
        private FragmentTransaction transaction;  
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
              
            et1 = (EditText) findViewById(R.id.et1);  
              
            manager = getFragmentManager();  
            transaction = manager.beginTransaction();  
              
            transaction.add(R.id.contents, new Fragment1());  
            transaction.commit();  
              
        }  
          
        //点击按钮时  进行传值  
        public void onclickBtn(View v){  
            switch (v.getId()) {  
            case R.id.button1:  
                Fragment1 fragment1 = new Fragment1();  
                Bundle bundle = new Bundle();  
                String strValue = et1.getText().toString().trim();  
                bundle.putString("str", strValue);  
                fragment1.setArguments(bundle);  
                //如果transaction  commit()过  那么我们要重新得到transaction  
                transaction = manager.beginTransaction();  
                transaction.replace(R.id.contents, fragment1);  
                transaction.commit();  
                break;  
      
            default:  
                break;  
            }  
        }  
          
          
          
    }  

Fragment :

    package com.qianfeng.activitytofragment;  
      
    import android.annotation.SuppressLint;  
    import android.app.Fragment;  
    import android.os.Bundle;  
    import android.view.LayoutInflater;  
    import android.view.View;  
    import android.view.ViewGroup;  
    import android.widget.TextView;  
      
    @SuppressLint("NewApi")   
    public class Fragment1 extends  Fragment{  
        @Override  
        public View onCreateView(LayoutInflater inflater, ViewGroup container,  
                Bundle savedInstanceState) {  
            // TODO Auto-generated method stub  
            View view = inflater.inflate(R.layout.fragment_layout1, container, false);  
            TextView textView1 = (TextView) view.findViewById(R.id.textView1);  
            Bundle bundle = getArguments();//从activity传过来的Bundle  
            if(bundle!=null){  
                textView1.setText(bundle.getString("str"));  
            }  
              
            return view;  
        }  
    }  


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:422565次
    • 积分:6984
    • 等级:
    • 排名:第3670名
    • 原创:287篇
    • 转载:36篇
    • 译文:0篇
    • 评论:81条
    坚持不懈
    如果这篇文章帮助了你,可以点入下面的连接,帮忙点击两个广告,给点鼓励,Thanks♪(・ω・)ノ !!!
    VR次元
    最新评论