当前位置: 编程技术>移动开发
本页文章导读:
▪Core Data浅谈系列之5 : 在UITableView中展示 Core Data浅谈系列之五 : 在UITableView中展示
首先,为App添加导航栏:
- (NSArray *)fetchPlayerList
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *teamEntity = [NSEntityDescription enti.........
▪ Pract03 Intent的运用与多个Activity的交互 Pract03 Intent的应用与多个Activity的交互实机测试界面截图如下:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="folyd.pract0.........
▪ DatePickerDialog日期范畴的控制 DatePickerDialog日期范围的控制我用的是自定义dialog实现的,在DatePicker的onDateChangedListentr事件中实现范围的控制
代码如下:
public class MainActivity extends Activity {
/** Called when the activity is first created.........
[1]Core Data浅谈系列之5 : 在UITableView中展示
来源: 互联网 发布时间: 2014-02-18
Core Data浅谈系列之五 : 在UITableView中展示
在逻辑上(表关系)将Team和Player关联起来后,我们将其展现到UI视图上。
首先,为App添加导航栏:
@interface AppDelegate : UIResponder <UIApplicationDelegate >
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *navController;
@property (strong, nonatomic) ViewController *viewController;
@end
@implementation AppDelegate
- (void)dealloc
{
[_window release];
[_navController release];
[_viewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
self.navController = [[[UINavigationController alloc] initWithRootViewController:self.viewController] autorelease];
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
return YES;
}
然后在ViewController上添加一个UITableView,布局好并实现如下相应的代理函数:
#pragma mark -
#pragma mark - UITableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.teamArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"TeamTableViewCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (nil == cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];
}
Team *teamObject = [self.teamArray objectAtIndex:indexPath.row];
UIImage *nbaImage = [UIImage imageNamed:@"nba@2x.jpg"];
cell.imageView.image = nbaImage;
cell.imageView.backgroundColor = [UIColorredColor];
cell.textLabel.text = teamObject.name;
cell.detailTextLabel.text = teamObject.city;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#pragma mark -
#pragma mark - UITableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
Team *teamObject = [self.teamArray objectAtIndex:indexPath.row];
PlayerListViewController *playerListVC = [[[PlayerListViewController alloc] init] autorelease];
playerListVC.team = teamObject;
playerListVC.cdViewController = self;
[self.navigationController pushViewController:playerListVC animated:YES];
}
在插入一些球队信息后,可以得到如下效果(按球队名称排序):
- (NSArray *)fetchTeamList
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *teamEntity = [NSEntityDescription entityForName:@"Team" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:teamEntity];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name"ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSError *error = NULL;
NSArray *array = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (error) {
NSLog(@"Error : %@\n", [error localizedDescription]);
}
[fetchRequest release], fetchRequest = nil;
return array;
}
点击cell,就进入到该队的球员列表:
- (NSArray *)fetchPlayerList
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *teamEntity = [NSEntityDescription entityForName:@"Player" inManagedObjectContext:self.cdViewController.managedObjectContext];
[fetchRequest setEntity:teamEntity];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"age"ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"team == %@", self.team];
[fetchRequest setPredicate:predicate];
NSError *error = NULL;
NSArray *array = [self.cdViewController.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (error) {
NSLog(@"Error : %@\n", [error localizedDescription]);
}
[fetchRequest release], fetchRequest = nil;
return array;
}
通过导航栏右边的Add按钮来添加球员信息:
- (IBAction)addBtnDidClick:(id)sender
{
// We don't check the user input.
Player *playerObject = [NSEntityDescription insertNewObjectForEntityForName:@"Player" inManagedObjectContext:self.cdViewController.managedObjectContext];
playerObject.name = self.nameTextField.text;
playerObject.age = [NSNumber numberWithInteger:[self.ageTextField.text integerValue]];
playerObject.team = self.team;
[self.cdViewController saveContext];
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)cancelBtnDidClick:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
}
以上对NSManagedObject的操作都位于同一份NSManagedObjectContext中。如上面添加球员的函数addBtnDidClick:所注释的,添加球员信息时并没有对数据进行验证 —— 这将在下一篇讨论。
Brief Talk About Core Data Series, Part 5 : Showing in UITableView
Jason Lee @ Hangzhou
Blog : http://blog.csdn.net/jasonblog
Weibo : http://weibo.com/jasonmblog
[2] Pract03 Intent的运用与多个Activity的交互
来源: 互联网 发布时间: 2014-02-18
Pract03 Intent的应用与多个Activity的交互
OtherAct.java
other.xml
string.xml
R.java
实机测试界面截图如下:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="folyd.pract03"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="folyd.pract03.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".OtherAct" android:label="@string/otname">
</activity>
</application>
</manifest>
MainActivity.java
package folyd.pract03;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Button;
public class MainActivity extends Activity {
private TextView tv=null;
private Button btn=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.myTextview);
btn=(Button)findViewById(R.id.myButton);
tv.setText(R.string.textview);
btn.setText(R.string.button);
btn.setOnClickListener(new myButtonListener());//设置Button按钮的监听器
}
class myButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();//申明Intent对象
intent.setClass(MainActivity.this, OtherAct.class);//调用Intent类的setClass() 方法设置要跳转到哪个Activity
MainActivity.this.startActivity(intent);//调用Activity的startActivity() 方法
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}OtherAct.java
package folyd.pract03;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class OtherAct extends Activity{
private TextView tv=null;
private Button btn=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
tv=(TextView)findViewById(R.id.myTextview);
btn=(Button)findViewById(R.id.myButton);
tv.setText(R.string.text2);
btn.setText(R.string.send);
btn.setOnClickListener(new OnClickListener() {//设置Button按钮的监听器,与MainActivity.java 类似
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Uri uri=Uri.parse("smsto:1308008000");//设置要发送的手机号码
Intent intent=new Intent(Intent.ACTION_SENDTO,uri);
intent.putExtra("sms_body", "我已经成功安装了你的Android应用程序了哦。");
startActivity(intent);
}
});
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#6699aa"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/myTextview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>other.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/myTextview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Pract03</string>
<string name="otname">OtherActivity</string>
<string name="textview">Touch it to start an activity.</string>
<string name="button">Confirm</string>
<string name="menu_settings">Settings</string>
<string name="text2">This is the second activity.</string>
<string name="send">SendMSG</string>
</resources>R.java
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package folyd.pract03;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int menu_settings=0x7f070002;
public static final int myButton=0x7f070001;
public static final int myTextview=0x7f070000;
}
public static final class layout {
public static final int activity_main=0x7f030000;
public static final int other=0x7f030001;
}
public static final class menu {
public static final int activity_main=0x7f060000;
}
public static final class string {
public static final int app_name=0x7f040000;
public static final int button=0x7f040003;
public static final int menu_settings=0x7f040004;
public static final int otname=0x7f040001;
public static final int send=0x7f040006;
public static final int text2=0x7f040005;
public static final int textview=0x7f040002;
}
public static final class style {
/**
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
API 11 theme customizations can go here.
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
API 14 theme customizations can go here.
*/
public static final int AppBaseTheme=0x7f050000;
/** Application theme.
All customizations that are NOT specific to a particular API-level can go here.
*/
public static final int AppTheme=0x7f050001;
}
}
[3] DatePickerDialog日期范畴的控制
来源: 互联网 发布时间: 2014-02-18
DatePickerDialog日期范围的控制
我用的是自定义dialog实现的,在DatePicker的onDateChangedListentr事件中实现范围的控制
代码如下:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView text = (TextView) findViewById(R.id.text);
LayoutInflater l = LayoutInflater.from(this);
View v = l.inflate(R.layout.dialog, null);
final DatePicker datePicker = (DatePicker) v
.findViewById(R.id.datepicker);
datePicker.init(2000, 1, 3, new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
if (isDateAfter(view)) {
view.init(2000, 11, 30, this);
}
if (isDateBefore(view)) {
view.init(1949, 11, 30, this);
}
}
private boolean isDateAfter(DatePicker tempView) {
if (tempView.getYear() > 2000) {
return true;
} else
return false;
}
private boolean isDateBefore(DatePicker tempView) {
if (tempView.getYear() < 1921) {
return true;
} else
return false;
}
});
Dialog dialog = new AlertDialog.Builder(this)
.setTitle(
datePicker.getYear() + "年"
+ (datePicker.getMonth() + 1) + "月"
+ datePicker.getDayOfMonth() + "日")
.setView(v)
.setIcon(R.drawable.ic_dialog_time)
.setNeutralButton("设置", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
text.setText(datePicker.getYear() + "年"
+ (datePicker.getMonth() + 1) + "月"
+ datePicker.getDayOfMonth() + "日");
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create();
dialog.show();
}
}
自定义的dialog.xml代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<DatePicker
android:id="@+id/datepicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
以上在3.0版本之前支持,在3.0版本以后请使用下面的方法
public void onClick(View v) {
switch (answer.type) {
case 1:// 日期类型
DatePickerDialog dialog = new DatePickerDialog(ctx, this, 1980, 0,
1);
DatePicker datePicker = dialog.getDatePicker();
datePicker.setMinDate(DateUtils.getInstance().format("1970-01-01")
.getTime());
datePicker.setMaxDate(DateUtils.getInstance().format("2013-01-07")
.getTime());
dialog.show();
break;
}
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
editText.setText(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth
+ "日");
}
最新技术文章: