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