February 14 learning content

Today is yesterday watched video knocking writing code,

In various interface data transmitted by the method Bundle; 

Written in the onClick method

 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);

The value passed to the interface B, B when receiving interface

 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);

The value B is then spread again in A;

The need to write code 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();
            }
        });

Then A is startActivity (intent); modify startActivityForResult (intent, 0);

Then add a function

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

B is transferred to the A in the title;

Also learned first use of the Fragment;

Creating a container of Activity, and then create two class inherits 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();
            }
        });

        // instantiate AFragment 
        afrgment = new new Afragment ();
         // put AFragment Activity added to the specified position 
        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, to false ); // to a layout file 
        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, to false ); // to a layout file 
        return View;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {

        mtvtitle= (TextView) view.findViewById(R.id.tv_title);

    }
}
BFragment

Used to replace the fragment of the interface; tomorrow to continue learning courses fragment of the other three;

Guess you like

Origin www.cnblogs.com/1234yyf/p/12307954.html