上篇博主要记录了基于window-base-application的第一个界面显示,接下去想做的按钮功能实现!!
首先我们要新建一个UIViewController类(.xib资源文件也勾选),开发过程中我们可能对UIView和UIViewController这2个类概念比较模糊,按我现在的肤浅理解为UIView就是类似JAVA里面的jsp界面,而UIViewController则类似JAVA中Servlet或者Struts这个角色,在UIViewController中可以用代码来实现UIView(类似Swing编程),而在Servlet中也是可以用纯代码来写jsp页面同样的道理!
在这个头文件中我们额外增加了一个<UITextFieldDelegate>协议,这个协议的作用就是防止键盘遮挡界面,按下键盘的Return/Done键自动隐藏的功能。
我们在申明的成员变量前面增加了IBOutlet,login方法的返回值设置为IBAction 这2个关键字的作用是告诉Interface Bulider这些成员变量、方法是暴露给Interface Bulider的,在界面设计的时候可以引用
在实现类里面我们实现了头文件中的login方法:
-(IBAction) login{
NSString *nameString=userName.text;
NSString *pdString=passWord.text;
//判断用户输入是否为空值
if (nameString.length==0||pdString.length==0) {
NSLog(@"输入姓名为:%@,密码为:%@",userName.text,passWord.text);
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:
@"(名字/密码)不能为空" message:@"请输入用户名和密码后点击登入!" delegate:nil
cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alertView show];
[alertView release];
label.text=nil;
return;
}
//判断用户名是否超过10个字符
if (nameString.length>10) {
userName.text=[userName.text substringToIndex:10];
label.textColor=[UIColor redColor];
label.text=@"用户名太长!";
return;
}
}
该方法体主要是对登录界面的参数进行判断
然后我们点开首个界面的xib,右键点开新建的controller如下图所示,刚才在头文件中申明的变量和方法都会显示。点中圆圈后分别和界面上的用户名、密码、登录等进行关联
然后上面提到在输入用户名和密码时会挡住界面,我们需要在界面中需要设置delegate,让<UITextFieldDelegate>监听到键盘输入事件,设置如下
密码输入框也需要设置
并在实现类中增加以下代码,当用户输入结束后点击按钮盘的Done/Return就会自动隐藏键盘
//输入框增加自动隐藏键盘事件
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
至此我们的一个基于window-base-application的简单登录以及事件响应功能都已经完成。下篇就将介绍另外一种View-Base-application类型的模版!
下面通过一个实例演示如何创建、启动、停止及绑定一个Service,具体步骤:
1、创建一个工程,在main.xml中声明四个Button,分别用来启动Service、停止Service、绑定Service和接触绑定Service
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:id="@+id/startButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="启动"/>
<Button android:id="@+id/stopButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="停止"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:id="@+id/bindButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="绑定"/>
<Button android:id="@+id/unbindButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="解除绑定"/>
</LinearLayout>
</LinearLayout>
2、创建一个MyService,继承自Service,覆盖其生命周期中的方法(onCreate、onStart、onDestroy),并在各个方法中显示信息
public class MyService extends Service{
/* (non-Javadoc)
* @see android.app.Service#onBind(android.content.Intent)
*/
@Override
public IBinder onBind(Intent intent) {
Log.i("Service", "onBind...");
Toast.makeText(MyService.this, "OnBind...", Toast.LENGTH_LONG).show() ;
return null;
}
@Override
public void onCreate() {
Log.i("Service", "onCreate...");
Toast.makeText(MyService.this, "onCreate...", Toast.LENGTH_LONG).show() ;
super.onCreate();
}
@Override
public void onDestroy() {
Log.i("Service", "onDestroy...");
Toast.makeText(MyService.this, "onDestroy...", Toast.LENGTH_LONG).show() ;
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
Log.i("Service", "onStart...");
Toast.makeText(MyService.this, "onStart...", Toast.LENGTH_LONG).show() ;
super.onStart(intent, startId);
}
}
3、在AndroidManifest.xml配置文件中声明activity和service
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.newcosoft.service"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".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>
<service android:name="MyService">
<intent-filter>
<action android:name="com.newcosoft.service.action.MY_SERVICE"></action>
</intent-filter>
</service>
</application>
</manifest>
4、接下来在MainActivity分别创建四个OnClickListener监听和ServiceConnection
public class MainActivity extends Activity {
private Button startButton,stopButton,bindButton,unbindButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startButton = (Button) this.findViewById(R.id.startButton);
stopButton = (Button) this.findViewById(R.id.stopButton);
bindButton = (Button) this.findViewById(R.id.bindButton);
unbindButton = (Button) this.findViewById(R.id.unbindButton);
startButton.setOnClickListener(startOnClickListener);
stopButton.setOnClickListener(stopOnClickListener);
bindButton.setOnClickListener(bindOnClickListener);
unbindButton.setOnClickListener(unbindOnClickListener);
}
//启动Service监听器
private OnClickListener startOnClickListener = new OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.newcosoft.service.action.MY_SERVICE");
startService(intent);
}
};
//停止Service监听器
private OnClickListener stopOnClickListener = new OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.newcosoft.service.action.MY_SERVICE");
stopService(intent);
}
};
//创建连接对象
private ServiceConnection conn = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("MainActivity", "onServiceConnected");
Toast.makeText(MainActivity.this, "连接成功!", Toast.LENGTH_LONG);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("MainActivity", "onServiceDisconnected");
Toast.makeText(MainActivity.this, "断开连接成功!", Toast.LENGTH_LONG);
}
};
//绑定Service监听器
private OnClickListener bindOnClickListener = new OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.newcosoft.service.action.MY_SERVICE");
//指定自动创建
bindService(intent, conn, BIND_AUTO_CREATE);
}
};
//接触绑定Service监听器
private OnClickListener unbindOnClickListener = new OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.newcosoft.service.action.MY_SERVICE");
unbindService(conn);
}
};
}
运行结果如下:
AppWidget(2)控件监听器绑定
什么是PendingIntent
PendingIntent创建之后并不马上使用 PendingIntent将实际Intent包裹其中
AppWidget和应用程序运行在2个进程中
进程A将PendingIntent交给进程B 当B中发生某事件 则其中的Intent被执行
创建PendingIntent的方法 (PendingIntent类的静态方法)
1 getActivity(...)
2 getBroadcast(...)
3 getService(...)
RemoteViews的作用
RemoteViews对象表示了一系列的View对象(非应用程序同一进程的)
RemoteViews所表示的对象运行在另外的进程当中
AppWidget对于Activity就是一个RemoteViews
在AppWidget中使用控件
在ApppWidget中添加控件 例如一个Button 在layout中声明
为Button绑定处理器
AppWidget和应用程序不在同一进程中 要用:
remoteViews.setOnClickPendingIntent(R.id.widgetButtonId,pendingIntent);
主要在onUpdate方法中 其中有一个参数int[] appWidgetId 是桌面上创建的widget
// 1 创建一个Intent Intent intent = new Intent(context,TargetActivity.class); // 2 创建一个PendingIntent PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); // 3 得到RemoteViews RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.example_appwidget); // 4 设置事件 绑定处理器 第一个参数是被绑定处理器控件的ID 第二个为动作 remoteViews.setOnClickPendingIntent(R.id.widgetButtonId, pendingIntent); // 5 更新AppWidget 第一个参数指定更新哪一个 第二个参数为更新的控件 appWidgetManager.updateAppWidget(appWidgetIds[i],remoteViews);
最终实现的是AppWidget上点击一个Button 跳转到TargetActivity