前面分别学到了Tag Bar和Navigation的使用,这次我们把他合起来使用,效果如下图
首先创建项目,选择window based application。项目名称为Nav_Tagbar。在.h中完成代码
#import <UIKit/UIKit.h> @interface Nav_TagbarAppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; UITabBarController *rootController; } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UITabBarController *rootController; @end
在.m中完成代码
#import "Nav_TagbarAppDelegate.h" @implementation Nav_TagbarAppDelegate @synthesize window; @synthesize rootController; #pragma mark - #pragma mark Application lifecycle - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [window addSubview:rootController.view]; [window makeKeyAndVisible]; return YES; } - (void)dealloc { [rootController release]; [window release]; [super dealloc]; } @end
再创建三个类,名为navView,secondView,thirdView,并生成视图。下面进行页面设计。双击MainWindow.xib,首先将一个Tag Bar Controller拖入nib主窗口中,按住Ctrl将Nav Tagbar App Delegate拖入Tag Bar Controller。之后将一个Navigation Controller拖入Tag Bar Controller中,如图
再做好相对界面的视图和控制器选择。这是nav的选择
在navView.xib中生成一个按钮,实现入栈跳转。在相应的.h.m文件中实现如下代码
#import <UIKit/UIKit.h> @interface navView : UIViewController { } -(IBAction)buttonPressed; @end
#import "navView.h" #import "thirdView.h" @implementation navView -(IBAction)buttonPressed{ thirdView *mythirdView = [[thirdView alloc] initWithNibName:@"thirdView" bundle:nil]; [self.navigationController pushViewController:mythirdView animated:YES]; [mythirdView release]; }
实现ListView背景
ListView
这里实现了1、点击ListView中的Item都会改变此Item的背景2、使用Theme自定义Item选择器,也就是当选中一个item的时候,此item的背景改变
文件说明:
ListDemo.java -------Activity类
res/layout/list.xml ------布局文件
res/layout/listitem.xml ------每一个item的布局
res/drawable/addtion.png -----随便找的做,用做点击item时候设置背景的图片
res/drawable/icon.png ----随便找的图片,用于选择器
res/values/styles.xml ------风格样式表文件
ListDemo.java
--------------------------------------------
package cc.androidos.layout; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; /** * * @author Wang XinFeng * @version 1.0 * @since 2009-3-24 */ public class ListDemo extends Activity { @Override protected void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ); //如果你设置了此项,那么就会改变listView 的选择器 setTheme( R.style.theme ); setContentView( R.layout.list ); final ListView lv = ( ListView ) findViewById( R.id.ListView01 ); ArrayAdapter<String> ss = new ArrayAdapter<String>(this,R.layout.listitem,new String[]{"A","B"}); lv.setAdapter( ss ); lv.setOnItemClickListener( new AdapterView.OnItemClickListener(){ //点击item的事件监听器 @Override public void onItemClick( AdapterView<?> arg0, View arg1, int arg2, long arg3 ) { //arg1实际上就是你点击的那个item的组件对象 //在这里直接设置它的背景 arg1.setBackgroundResource( R.drawable.addtion ); } } ); } }
--------------------------------------------
res/layout/list.xml
--------------------------------------------
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" androidrientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- 这里是简单的ListView --> <ListView android:id="@+id/ListView01" android:layout_width="fill_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout>
--------------------------------------------
res/layout/listitem.xml
<TextView android:id="@+id/TextView01" android:layout_width="fill_parent" android:layout_height="100px" xmlns:android="http://schemas.android.com/apk/res/android"> </TextView>
res/values/styles.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="theme" parent="android:Theme"> <!--设置ListView到整个Theme中--> <item name="android:listViewStyle">@style/listS</item> </style> <!--配置ListView 的 Theme--> <style name="listS" parent="android:style/Widget.AbsListView"> <item name="android:listSelector">@drawable/icon</item> </style> </resources>
--------------------------------------------
图片放进去有点丑陋,这里仅仅做为演示(演示结果为我写的另一个例子)。
ListView背景:
点击item设置的背景:
暂时的
@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
// Store UI state to the savedInstanceState.
// This bundle will be passed to onCreate on next call.
EditText txtName = (EditText)findViewById(R.id.txtName);
String strName = txtName.getText().toString();
EditText txtEmail = (EditText)findViewById(R.id.txtEmail);
String strEmail = txtEmail.getText().toString();
CheckBox chkTandC = (CheckBox)findViewById(R.id.chkTandC);
boolean blnTandC = chkTandC.isChecked();
savedInstanceState.putString("Name", strName);
savedInstanceState.putString("Email", strEmail);
savedInstanceState.putBoolean("TandC", blnTandC);
super.onSaveInstanceState(savedInstanceState);
}
灰度
恢复
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Restore UI state from the savedInstanceState.
if (savedInstanceState != null)
{
String strValue = savedInstanceState.getString("Name");
if (strValue != null)
{
EditText oControl = (EditText)findViewById(R.id.txtName);
oControl.setText(strValue);
}
strValue = savedInstanceState.getString("Email");
if (strValue != null)
{
EditText oControl = (EditText)findViewById(R.id.txtEmail);
oControl.setText(strValue);
}
CheckBox chkTandC = (CheckBox)findViewById(R.id.chkTandC);
chkTandC.setChecked(savedInstanceState.getBoolean("TandC"));
}
}
持续的
@Override
protected void onPause()
{
super.onPause();
// Store values between instances here
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
// Put the values from the UI
EditText txtName = (EditText)findViewById(R.id.txtName);
String strName = txtName.getText().toString();
EditText txtEmail = (EditText)findViewById(R.id.txtEmail);
String strEmail = txtEmail.getText().toString();
CheckBox chkTandC = (CheckBox)findViewById(R.id.chkTandC);
boolean blnTandC = chkTandC.isChecked();
editor.putString("Name", strName); // value to store
editor.putString("Email", strEmail); // value to store
editor.putBoolean("TandC", blnTandC); // value to store
// Commit to storage
editor.commit();
}/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Get the between instance stored values
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
// Set the values of the UI
EditText oControl = (EditText)findViewById(R.id.txtName);
oControl.setText(preferences.getString("Name", null));
oControl = (EditText)findViewById(R.id.txtEmail);
oControl.setText(preferences.getString("Email", null));
CheckBox chkTandC = (CheckBox)findViewById(R.id.chkTandC);
chkTandC.setChecked(preferences.getBoolean("TandC", false));
}
前一种主要是在旋转凭 以及登录信息,也就是说app关闭则保存状态小时,activity切换没有关系不影响
后一种关掉重启 继续保留