Android安卓的牛刀小试-Intent的认识-Bundle的数据传递-Fragment的基本操作

1>.Intent的作用:连接多个Activity,Activity之间的媒介。

作用:

1.*启动一个新的Activity

Intent intent=new Intent(MainActivity.this,TestDemo1.class);
startActivity(intent);

finish()销毁一个Activity

主类方法:

package com.alian.demo1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		
		Button button=(Button) findViewById(R.id.qiehuan);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(MainActivity.this,TestDemo1.class);
				startActivity(intent);
			}
		});
		

	}

	
}

2>.Bundle在Activity之间数据传递

存值:

Intent intent=new Intent(MainActivity.this,TestDemo1.class);
                Bundle bundle= new Bundle();
                bundle.putSerializable("hello", "hello world");
                intent.putExtras(bundle);
                startActivity(intent);

取值:

    Intent intent =getIntent();
        
        Bundle bundle=intent.getExtras();
        String test=bundle.getString("hello");

3>.Fragment的用法

Fragment是Android3.0以后引入的新的api,为了适配大屏的平板。

在普通手机开发的过程中,使用Fragment能实现一个界面的多次使用,能加快效率。Fragment可以被认为是Activity界面的一个布局,其依赖于Activity,但是拥有自己的活动事件与生命周期。可以通过替换Activity中的Fragment实现界面的优化处理。

1.标签使用

<RelativeLayout
    android:background="@color/colorAccent"
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <Button
        android:id="@+id/fragment_Btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="One"
        />
</RelativeLayout>
public class Fragment_One extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_one,container,false);
        return view;
    }
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragment.Fragment_Main">
  <fragment
    android:id="@+id/main_fragment"
    android:name="com.example.zhang.androidtestdemo01.Fragment.Fragment_One"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
</FrameLayout>

2.动态添加

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragment.Fragment_Main">
    <FrameLayout
        android:id="@+id/fragment_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>
<Button
    android:id="@+id/main_btn"
    android:layout_width="match_parent"
    android:text="click to fragment two"
    android:layout_height="40dp" />
</FrameLayout>
public class Fragment_Main extends AppCompatActivity{
    private Fragment_One mFOne;
    private Fragment_Two mFTwo;
    private Button mBtn;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment__main);
 
        mFOne=new Fragment_One();
        FragmentManager fm=getFragmentManager();
        FragmentTransaction tx=fm.beginTransaction();
        tx.add(R.id.fragment_content,mFOne,"ONE");
        tx.commit();
 
        mBtn=findViewById(R.id.main_btn);
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mFTwo=new Fragment_Two();
                FragmentManager fm=getFragmentManager();
                FragmentTransaction tx=fm.beginTransaction();
                tx.replace(R.id.fragment_content,mFTwo,"Two");
                tx.commit();
            }
        });
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿联爱学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值