杩欏嚑澶╀竴鐩村湪淇敼twigee鐨勬簮浠g爜锛屽叾涓竴涓鍔犲叆鐨勫姛鑳芥槸甯搁┗Notification鏍忥紝浠ュ墠鍐欑殑鏃跺€欏彧鑳藉嚭鐜?鍦ㄢ€滈€氱煡鈥濊繖涓€缁勪腑锛屾兂鎶婂畠鏀惧湪鈥滄鍦ㄨ繍琛屸€濈粍涓嵈涓嶇煡閬撴€庝箞鏀撅紝鏌ヤ簡涓嬪畼鏂规枃妗o紝鎵惧埌浜嗘柟娉曪紝鍦╪otification鐨刦lags瀛楁涓姞涓€涓?鈥淔LAG_ONGOING_EVENT鈥濆氨鍙互浜嗐€傚悓鏃舵垜涔熸妸Notification鐨勪娇鐢ㄦ柟娉曠粰鎬荤粨浜嗕竴涓嬨€傝瑙佷笅鏂囷細
(1)銆佷娇鐢ㄧ郴缁熷畾涔夌殑Notification浠ヤ笅鏄娇鐢ㄧず渚嬩唬鐮侊細
//鍒涘缓涓€涓狽otificationManager鐨勫紩鐢?/p>
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);
//瀹氫箟Notification鐨勫悇绉嶅睘鎬?/p>
int icon = R.drawable.icon; //閫氱煡鍥炬爣
CharSequence tickerText = "Hello"; //鐘舵€佹爮鏄剧ず鐨勯€氱煡鏂囨湰鎻愮ず
long when = System.currentTimeMillis(); //閫氱煡浜х敓鐨勬椂闂达紝浼氬湪閫氱煡淇℃伅閲屾樉绀?/p>
//鐢ㄤ笂闈㈢殑灞炴€у垵濮嬪寲Nofification
Notification notification = new Notification(icon,tickerText,when);
/*
* 娣诲姞澹伴煶
* notification.defaults |=Notification.DEFAULT_SOUND;
* 鎴栬€呬娇鐢ㄤ互涓嬪嚑绉嶆柟寮?/p>
* notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
* notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
* 濡傛灉鎯宠璁╁0闊虫寔缁噸澶嶇洿鍒扮敤鎴峰閫氱煡鍋氬嚭鍙嶅簲锛屽垯鍙互鍦╪otification鐨刦lags瀛楁澧炲姞"FLAG_INSISTENT"
* 濡傛灉notification鐨刣efaults瀛楁鍖呮嫭浜?DEFAULT_SOUND"灞炴€э紝鍒欒繖涓睘鎬у皢瑕嗙洊sound瀛楁涓畾涔夌殑澹伴煶
*/
/*
* 娣诲姞鎸姩
* notification.defaults |= Notification.DEFAULT_VIBRATE;
* 鎴栬€呭彲浠ュ畾涔夎嚜宸辩殑鎸姩妯″紡锛?/p>
* long[] vibrate = {0,100,200,300}; //0姣鍚庡紑濮嬫尟鍔紝鎸姩100姣鍚庡仠姝紝鍐嶈繃200姣鍚庡啀娆℃尟鍔?00姣
* notification.vibrate = vibrate;
* long鏁扮粍鍙互瀹氫箟鎴愭兂瑕佺殑浠讳綍闀垮害
* 濡傛灉notification鐨刣efaults瀛楁鍖呮嫭浜?DEFAULT_VIBRATE",鍒欒繖涓睘鎬у皢瑕嗙洊vibrate瀛楁涓畾涔夌殑鎸姩
*/
/*
* 娣诲姞LED鐏彁閱?/p>
* notification.defaults |= Notification.DEFAULT_LIGHTS;
* 鎴栬€呭彲浠ヨ嚜宸辩殑LED鎻愰啋妯″紡:
* notification.ledARGB = 0xff00ff00;
* notification.ledOnMS = 300; //浜殑鏃堕棿
* notification.ledOffMS = 1000; //鐏殑鏃堕棿
* notification.flags |= Notification.FLAG_SHOW_LIGHTS;
*/
/*
* 鏇村鐨勭壒寰佸睘鎬?/p>
* notification.flags |= FLAG_AUTO_CANCEL; //鍦ㄩ€氱煡鏍忎笂鐐瑰嚮姝ら€氱煡鍚庤嚜鍔ㄦ竻闄ゆ閫氱煡
* notification.flags |= FLAG_INSISTENT; //閲嶅鍙戝嚭澹伴煶锛岀洿鍒扮敤鎴峰搷搴旀閫氱煡
* notification.flags |= FLAG_ONGOING_EVENT; //灏嗘閫氱煡鏀惧埌閫氱煡鏍忕殑"Ongoing"鍗?姝e湪杩愯"缁勪腑
* notification.flags |= FLAG_NO_CLEAR; //琛ㄦ槑鍦ㄧ偣鍑讳簡閫氱煡鏍忎腑鐨?娓呴櫎閫氱煡"鍚庯紝姝ら€氱煡涓嶆竻闄わ紝
* //缁忓父涓嶧LAG_ONGOING_EVENT涓€璧蜂娇鐢?/p>
* notification.number = 1; //number瀛楁琛ㄧず姝ら€氱煡浠h〃鐨勫綋鍓嶄簨浠舵暟閲忥紝瀹冨皢瑕嗙洊鍦ㄧ姸鎬佹爮鍥炬爣鐨勯《閮?/p>
* //濡傛灉瑕佷娇鐢ㄦ瀛楁锛屽繀椤讳粠1寮€濮?/p>
* notification.iconLevel = ; //
*/
//璁剧疆閫氱煡鐨勪簨浠舵秷鎭?/p>
Context context = getApplicationContext(); //涓婁笅鏂?/p>
CharSequence contentTitle = "My Notification"; //閫氱煡鏍忔爣棰?/p>
CharSequence contentText = "Hello World!"; //閫氱煡鏍忓唴瀹?/p>
Intent notificationIntent = new Intent(this,Main.class); //鐐瑰嚮璇ラ€氱煡鍚庤璺宠浆鐨凙ctivity
PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
//鎶奛otification浼犻€掔粰NotificationManager
mNotificationManager.notify(0,notification);
濡傛灉鎯宠鏇存柊涓€涓€氱煡锛屽彧闇€瑕佸湪璁剧疆濂絥otification涔嬪悗锛屽啀娆¤皟鐢?setLatestEventInfo(),鐒跺悗閲嶆柊鍙戦€佷竴娆¢€氱煡鍗冲彲锛屽嵆鍐嶆璋冪敤notify()銆?/p>
(2)銆佷娇鐢ㄨ嚜瀹氫箟鐨凬otification瑕?鍒涘缓涓€涓嚜瀹氫箟鐨凬otification锛屽彲浠ヤ娇鐢≧emoteViews銆傝瀹氫箟鑷繁鐨勬墿灞曟秷鎭紝棣栧厛瑕佸垵濮嬪寲涓€涓猂emoteViews瀵硅薄锛岀劧鍚?灏嗗畠浼犻€掔粰Notification鐨刢ontentView瀛楁锛屽啀鎶奝endingIntent浼犻€掔粰contentIntent瀛楁銆備互涓嬬ず渚嬩唬鐮?鏄畬鏁存楠わ細
//1銆佸垱寤轰竴涓嚜瀹氫箟鐨勬秷鎭竷灞€ view.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">
<ImageView android:id="@+id/image" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_marginRight="10dp" />
<TextView android:id="@+id/text" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:textColor="#000" />
</LinearLayout>
//2銆佸湪绋嬪簭浠g爜涓娇鐢≧emoteViews鐨勬柟娉曟潵瀹氫箟image鍜宼ext銆傜劧鍚庢妸RemoteViews瀵硅薄浼犲埌contentView瀛楁
RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.view);
contentView.setImageViewResource(R.id.image,R.drawable.icon);
contentView.setTextViewText(R.id.text,鈥滺ello,this message is in a custom expanded view鈥?;
notification.contentView = contentView;
//3銆佷负Notification鐨刢ontentIntent瀛楁瀹氫箟涓€涓狪ntent(娉ㄦ剰锛屼娇鐢ㄨ嚜瀹氫箟View涓嶉渶瑕乻etLatestEventInfo()鏂规硶)
Intent notificationIntent = new Intent(this,Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);
notification.contentIntent = contentIntent;
//4銆佸彂閫侀€氱煡
mNotificationManager.notify(2,notification);
//浠ヤ笅鏄叏閮ㄧず渚嬩唬鐮?/strong>
//鍒涘缓涓€涓狽otificationManager鐨勫紩鐢?/p>
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);
//瀹氫箟Notification鐨勫悇绉嶅睘鎬?/p>
int icon = R.drawable.icon; //閫氱煡鍥炬爣
CharSequence tickerText = "Hello"; //鐘舵€佹爮鏄剧ず鐨勯€氱煡鏂囨湰鎻愮ず
long when = System.currentTimeMillis(); //閫氱煡浜х敓鐨勬椂闂达紝浼氬湪閫氱煡淇℃伅閲屾樉绀?/p>
//鐢ㄤ笂闈㈢殑灞炴€у垵濮嬪寲Nofification
Notification notification = new Notification(icon,tickerText,when);
RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.view);
contentView.setImageViewResource(R.id.image, R.drawable.iconempty);
contentView.setTextViewText(R.id.text, "Hello,this is JC");
notification.contentView = contentView;
Intent notificationIntent = new Intent(this,Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);
notification.contentIntent = contentIntent;
//鎶奛otification浼犻€掔粰NotificationManager
mNotificationManager.notify(0,notification);
腾讯的QQ和微信中都有这样的操作,对一个UITableView中的cell,向左滑动的时候,会在对应的cell右侧(iOS7.0样式发生了一点变化)出现一个“删除”按钮
要达到这样的效果,完全用系统的就可以,不需要自定义一个Button,加到cell后面,同时给cell加手势捕获这个操作。
注意:如果在cell加一个手势(测试中我用的Pan),那么cell系统自己的就不生效了。
生效主要依赖3个代理方法(要是说成一个也行)
(1)- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
这是最主要的方法,如果要显示“删除”,把这个“按钮”的响应事件写好。
(2)- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
“Allows customization of the editingStyle for a particular cell located at 'indexPath'. If not implemented, all editable cells will have UITableViewCellEditingStyleDelete set for them when the table has editing property set to YES.”
就像API中说的一样,“如果不实现,所有的cell(默认都是可编辑的)都会有UITableViewCellEditingStyleDelete这个类型”,所以不写这个方法,照样能出现。但是,如果你对indexPath的cell返回了其他Style,那就没有了。
所以,根据具体逻辑和业务,灵活返回具体的Style
(3)- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
“Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable.”
在(2)中已经提到了,所有的cell都是默认可编辑的
总结:如果要单纯的实现“滑动出现删除按钮”,只需要实现(1)就OK了。
通过(2)(3)的配合,完成相应的逻辑和业务。
至于调用顺序:
(3)是在UITableViewDataSource中的,table显示的时候就调用了,处在第一响应位。
(2)如果某一行可以编辑,会调用这个返回具体的Style。
(1)则是点击了“按钮”以后才会进的。
附:
如果要更改按钮的title,很简单实现另外一个代理方法。
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
搜索Activity的java内容虽然不多,只是找到两个gridView然后分别给他们设置适配器,但是布局文件里面包含的信息还是很丰富的。
SearchActivity:
public class SearchActivity extends Activity { GridView mHotGridView, mHistoryGridView; //热门搜索和搜索历史 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.search_activity); prepareView(); } private void prepareView() { mHotGridView = (GridView) findViewById(R.id.hot_search_grid); mHotGridView.setAdapter(new GridAdapter(this)); mHistoryGridView = (GridView) findViewById(R.id.history_search_grid); mHistoryGridView.setAdapter(new GridAdapter(this)); } }
接下来是适配器GridAdapter的代码,学习了gridView的话应该是轻车熟路了,把test1里面的倒霉熊什么的都写上去:
public class GridAdapter extends BaseAdapter { Context mContext; String[] test1 = new String[] { "开心魔法", "奋斗", "导火索", "碟中谍4", "碟中碟3", "美人心计", "倒霉熊", "火影忍者", "喜洋洋", "北京爱情故事" }; public GridAdapter(Context cnt) { this.mContext = cnt; } @Override public int getCount() { // TODO Auto-generated method stub return test1.length; } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } @Override public View getView(int arg0, View arg1, ViewGroup arg2) { // TODO Auto-generated method stub if (arg1 == null) { arg1 = LayoutInflater.from(mContext).inflate( R.layout.search_grid_item, null); } TextView tv = (TextView) arg1.findViewById(R.id.title); tv.setText(test1[arg0]); return arg1; } }
布局文件里面用到的东西很多了,我把重点的讲下transparent这是图片重叠透明穿透,就是两张png的图片合在一块,不是覆盖。这里的效果就是摁下字,背景变了。
search_activity.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" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/phonetitlebar_bg" android:gravity="center" > <EditText android:id="@+id/editText1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/edittext_search" android:drawableLeft="@drawable/edittext_search_image" android:hint="@string/search_hit" android:paddingLeft="15dip" android:textSize="14dip" > </EditText> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_search_background" android:text="@string/search_text" /> </LinearLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/search_main_tree_bg" android:gravity="center_vertical" android:paddingLeft="15dip" android:text="@string/hot_search" android:textColor="#000000" android:textSize="18dip" /> <GridView android:id="@+id/hot_search_grid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="30dip" android:layout_marginRight="30dip" android:layout_marginTop="15dip" android:layout_weight="1" android:horizontalSpacing="50dip" android:numColumns="2" android:listSelector="@drawable/list_seletor_transparent" android:scrollbars="none"> </GridView> <LinearLayout android:id="@+id/linearLayout2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/search_main_tree_bg" android:gravity="center_vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingLeft="15dip" android:text="@string/search_history" android:textColor="#000000" android:textSize="18dip" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dip" android:background="@drawable/clear_but_background" android:text="@string/clear_search_history" android:textColor="#ffffff" /> </LinearLayout> <GridView android:id="@+id/history_search_grid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="30dip" android:layout_marginRight="30dip" android:layout_marginTop="15dip" android:layout_weight="1" android:horizontalSpacing="50dip" android:numColumns="2" android:listSelector="@drawable/list_seletor_transparent" android:scrollbars="none"> </GridView> </LinearLayout>
search_grid_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/hot_search_background" android:paddingLeft="3dip" android:paddingBottom="3dip" android:paddingTop="3dip"> <TextView android:id="@+id/title" android:layout_width="80dip" android:layout_height="wrap_content" android:singleLine="true"/> </LinearLayout>
PS:上面有些东西就没全部贴出来了,待会把整个源码贴上去,自己找了。