今天是将昨天所观看的视频进行代码的敲写,
数据在不同界面的传送,用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(); } }
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); } }
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); } }
用来更换fragment的界面;明天继续学习fragment的其他3个课程;