zoukankan      html  css  js  c++  java
  • 2月14日学习内容

    今天是将昨天所观看的视频进行代码的敲写,

    数据在不同界面的传送,用Bundle方法; 

    在onClick方法中写

     Intent intent=new Intent(AActivity.this,BActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putString("name","yyf");
                    bundle.putInt("age", 20);
                    intent.putExtras(bundle);
                    startActivity(intent);

    将值传到B界面,B界面的接收时候

     mtv1= (TextView) findViewById(R.id.tv_1);
            Bundle bundle=getIntent().getExtras();
            String name=bundle.getString("name");
            int age=bundle.getInt("age");
            mtv1.setText(name+","+age);

    然后将B 的值再传到A中;

    需要在B中写入代码

     mtv1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent();
                    Bundle bundle1=new Bundle();
                    bundle1.putString("title","哈哈哈哈");
                   intent.putExtras(bundle1);
                   setResult(Activity.RESULT_OK,intent);
                    finish();
                }
            });

    然后将A中的startActivity(intent);修改为startActivityForResult(intent,0);

    然后加上一个函数

     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            Toast.makeText(AActivity.this,data.getExtras().getString("title"),Toast.LENGTH_LONG).show();
        }

    B中的title会传送到A中;

    还学习了Fragment的第一节的用法;

    创建了一个container的Activity,然后创建两个class继承Fragment;

    package com.example.yangy.myapplication123.fragment;
    
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    import com.example.yangy.myapplication123.R;
    
    public class ContainerActivity extends ActionBarActivity {
    
        private Afragment afrgment;
        private Bfragment bfrgment;
        private Button mbutchange;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_container);
    
            mbutchange= (Button) findViewById(R.id.btn_change);
            mbutchange.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (bfrgment == null)
                    {
                        bfrgment=new Bfragment();
                    }
                    getFragmentManager().beginTransaction().replace(R.id.fl_container, bfrgment).commitAllowingStateLoss();
                }
            });
    
            //实例化AFragment
            afrgment=new Afragment();
            //把AFragment添加到Activity中指定位置
            getFragmentManager().beginTransaction().add(R.id.fl_container,afrgment).commitAllowingStateLoss();
    
        }
    }
    container
    package com.example.yangy.myapplication123.fragment;
    
    import android.app.Fragment;
    import android.support.annotation.Nullable;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    import com.example.yangy.myapplication123.R;
    
    public class Afragment extends Fragment {
        private TextView mtvtitle;
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
            View view=inflater.inflate(R.layout.activity_afragment,container,false);//给一个布局文件
            return view;
        }
    
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
    
            mtvtitle= (TextView) view.findViewById(R.id.tv_title);
            
        }
    }
    AFragment
    package com.example.yangy.myapplication123.fragment;
    
    import android.app.Fragment;
    import android.support.annotation.Nullable;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    import com.example.yangy.myapplication123.R;
    
    public class Bfragment extends Fragment{
    
        private TextView mtvtitle;
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
            View view=inflater.inflate(R.layout.activity_bfragment,container,false);//给一个布局文件
            return view;
        }
    
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
    
            mtvtitle= (TextView) view.findViewById(R.id.tv_title);
    
        }
    }
    BFragment

    用来更换fragment的界面;明天继续学习fragment的其他3个课程;

  • 相关阅读:
    Will Go eventually replace C++ as Google hoped when Go came out?
    5G到底什么时候来,它究竟能给我们带来什么?
    eog——Eye of GNOME Image Viewer
    Appweb——Embedded Web Server
    【2017】数字重排
    【9203】众数
    【2034】四人投票
    【9204】第k小整数
    【2031】求一元三次方程的解
    iOS 7: 如何为iPhone 5s编译64位应用
  • 原文地址:https://www.cnblogs.com/1234yyf/p/12307954.html
Copyright © 2011-2022 走看看