首先新建一个主页面activity_main.xml
界面如下,是要显示的主页面
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rpa_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
在menu文件夹下新建一个布局,显示为保存按钮
<menu 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"
tools:context="com.yhkj.jskf.forestpoliceproject.ForestPatrolWorkActivity" >
<!-- <item
android:id="@+id/action_forest_patrol_update"
android:orderInCategory="90"
android:title="@string/signature_update"
app:showAsAction="always" >
</item> -->
<item
android:id="@+id/action_forest_patrol_save"
android:orderInCategory="100"
android:title="保存"
android:enabled="false"
app:showAsAction="always" >
</item>
</menu>
主要代码如下
package com.yhkj.jskf.forestpoliceproject.activity.tanzhesi;
import java.util.ArrayList;
import java.util.Locale;
import org.json.JSONException;
import org.json.JSONObject;
import com.yhkj.jskf.forestpoliceproject.R;
import com.yhkj.jskf.forestpoliceproject.utils.MyAjaxCallback;
import com.yhkj.jskf.forestpoliceproject.utils.NetUtil;
import com.yhkj.jskf.forestpoliceproject.utils.NetworkConnections;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBar.TabListener;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
/**
* 添加林情
* */
public class LinQingAddActivity extends ActionBarActivity implements TabListener {
//自定义fragment的适配器
SectionsPagerAdapter mSectionsPagerAdapter;
//定义一个viewpager
ViewPager mViewPager;
private TextView tx;
private static Activity activity;
private static Intent intent;
private static EditText forest_area_et,// 林地面积(亩)
forest_cumulation,// 林地蓄积量
lawcase,// 涉林案件高发区
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
/主页面其实就是一个viewpager,加上actiongbar
setContentView(R.layout.activity_register_protected_animal);
activity = this;
/ / android.app.ActionBar ac1=getActionBar();如果没使用support包
final ActionBar actionBar = getSupportActionBar();//使用support包
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);//设置导航模式
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.rpa_pager);//整个内容区设置为viewPage
mViewPager.setAdapter(mSectionsPagerAdapter);// //设置滑动的adapter适配器里面的内容为mSectionsPagerAdapter,里面的内容为fragment
//对viewpager设置滑动时的监听函数
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
//滑动时,设置当前导航的页面
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
//重新选择时触发事件
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
//创建actionmenu布局
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.forest_patrol_work, menu);
return true;
}
private static MenuItem saveitem;
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
saveitem = menu.findItem(R.id.action_forest_patrol_save);
if (fcE != null) {
saveitem.setTitle(getString(R.string.signature_update));
} else {
saveitem.setTitle(getString(R.string.signature_save));
}
return true;
}
//设置选择时所触发的事件
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
//设置保存按钮的可不可以使用,以及使用的情况进行判断
if (tab.getPosition() == 1) {
if (saveitem != null) {
saveitem.setEnabled(true);
}
} else {
if (saveitem != null) {
saveitem.setEnabled(false);
}
}
//点击tab按钮时,设置viewpager的当前页面
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
//设置fragment的适配器,用来设置fragment
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
//为1的原因是, onCreateView的时候 case 值从 1开始 两个值为 1,2
//设置页面的默认值
return PlaceholderFragment.newInstance(position + 1);
}
//一共两个页面
@Override
public int getCount() {
// Show 3 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(" 基本信息").toUpperCase(l);
case 1:
return getString(" 查看").toUpperCase(l);
}
return null;
}
}
*/
//对fragment进行页面设置,进行创建页面,对页面进行初始化
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
// 相当于进行初始化
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1:
rootView = inflater.inflate(R.layout.activity_linqing_add_information, container,
false);
forest_area_et = (EditText) rootView.findViewById(R.id.forest_area_et);
forest_cumulation = (EditText) rootView.findViewById(R.id.forest_cumulation);
}
break;
default:
break;
}
return rootView;
case 2:
rootView = inflater.inflate(R.layout.remarks, container, false);
etNote = (EditText) rootView.findViewById(R.id.etNote); // 备注
break;
default:
break;
}
return rootView;
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}