package com.bwie.helpdemo; import com.bwie.fragment.BillFrag; import com.bwie.fragment.IndexFrag; import com.bwie.fragment.TaskFrag; import android.os.Bundle; import android.app.Activity; import android.content.SharedPreferences; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class IndexActivity extends FragmentActivity implements OnClickListener { private Button index_bill; private TextView index_home; private TextView index_task; private IndexFrag indexFrag; private BillFrag billFrag; private TaskFrag taskFrag; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_index); // 找控件 findView(); // 为布局添加fragment,开启事物 FragmentManager fm = getSupportFragmentManager(); FragmentTransaction tran = fm.beginTransaction(); indexFrag = new IndexFrag(); billFrag = new BillFrag(); taskFrag = new TaskFrag(); //R.id.relative为布局 tran.add(R.id.relative, indexFrag, "index").show(indexFrag) .add(R.id.relative, billFrag, "bill").hide(billFrag) .add(R.id.relative, taskFrag, "task").hide(taskFrag); tran.commit(); // 为按钮设置点击监听 index_bill.setOnClickListener(this); index_home.setOnClickListener(this); index_task.setOnClickListener(this); } private void findView() { index_bill = (Button) findViewById(R.id.index_bill); index_home = (TextView) findViewById(R.id.index_home); index_task = (TextView) findViewById(R.id.index_task); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.index_bill: // 获取事物 FragmentManager fm_1 = getSupportFragmentManager(); FragmentTransaction tran_0 = fm_1.beginTransaction(); tran_0.show(billFrag).hide(indexFrag).hide(taskFrag).commit(); break; case R.id.index_home: FragmentManager fm_2 = getSupportFragmentManager(); FragmentTransaction tran_1 = fm_2.beginTransaction(); tran_1.show(indexFrag).hide(billFrag).hide(taskFrag).commit(); break; case R.id.index_task: FragmentManager fm_3 = getSupportFragmentManager(); FragmentTransaction tran_2 = fm_3.beginTransaction(); tran_2.show(taskFrag).hide(billFrag).hide(indexFrag).commit(); break; } } }