首页 文章 精选 留言 我的

精选列表

搜索[桌面应用开发],共10000篇文章
优秀的个人博客,低调大师

【iOS 开发】防止 UIWindow 延迟释放占用状态栏

在展示 app 启动广告等情况下,我们可能需要在界面上叠加一个 UIWindow,但是实测情况下发现,由于 UIWindow 会被系统引用导致延迟释放,在 customWindow 实例被使用完之后,单纯 customWindow.rootViewController = [[UIViewController alloc] init]; customWindow = nil; 是不能让 customWindow 完全对当前界面没有影响的,其中有可能出现的一个问题是:app 使用各个 VC 独立管理状态栏的方式时,我们通过 [[UIViewController alloc] init] 创建的 VC 是有状态栏的,这是当 app 需要展示的其他 UIWindow 里面的 VC 没有状态栏时,customWindow 会浮在最顶层,强行叠加一个状态栏出来(iOS 9 亲测)。 这时改 keyWindow 或者设置 windowLevel 都是没有用的,各种尝试之后,解决方案是在把 window 设为 nil 之前,加一句 setHidden: customWindow.rootViewController = [[UIViewController alloc] init]; [customWindow setHidden:YES]; customWindow = nil; FYI.

优秀的个人博客,低调大师

iOS开发-仿大众点评iPad侧边导航栏

昨天其实已经写了一篇侧边栏的文章,不过感觉还不是很清晰,这篇文章算是补充吧,iPad上看了大众点评的侧边栏,基本上百分之九十类似,具体效果可参考下图: 对比昨天主要做了两个修改,一个是图片和文字的显示位置,另外一个就是关于底部的定位和设置的位置在横竖屏时显示的问题,侧边栏的区域是是自己控制的,需要注意一下横竖屏的时候设置一下autoresizingMask,底部图标定位的时候也是一样设置。 导航栏上每个按钮提取出了一个父类GPDockItem,头文件中的代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // // GPDockItem.h // GrouponProject //博客园FlyElephant:http://www.cnblogs.com/xiaofeixiang // Created by keso on 15/3/11. // Copyright (c) 2015年 keso. All rights reserved. // #import <UIKit/UIKit.h> @interface GPDockItem : UIButton -( void )imageSetting:( NSString *)backgroundImage selectedImage:( NSString *)selectedImage; @property ( nonatomic ,strong) NSString *title; //背景图片 @property ( nonatomic ,strong) NSString *backgroundImage; //选中图片 @property ( nonatomic ,strong) NSString *selectedImage; @end 相对于之前的代码,主要是添加了设置背景图片和设置选中图片的混合方法,定义了一个Title属性,之后的可以设置文字和图片的位置,重写两个方法: 1 2 3 4 5 6 7 8 9 10 11 12 13 //设置图片区域 -(CGRect)imageRectForContentRect:(CGRect)contentRect{ CGFloat width=contentRect.size.width; CGFloat height= contentRect.size.height * 0.7; return CGRectMake(0, 10, width, height); } //设置文字区域 -(CGRect)titleRectForContentRect:(CGRect)contentRect{ CGFloat width=contentRect.size.width; CGFloat height= contentRect.size.height * 0.3; CGFloat position=contentRect.size.height*0.7; return CGRectMake(0, position, width, height); } 设置背景图片和选中图片: 1 2 3 4 5 -( void )imageSetting:( NSString *)backgroundImage selectedImage:( NSString *)selectedImage{ self .backgroundImage=backgroundImage; self .selectedImage=selectedImage; } 设置显示文字和图片在区域内的位置: 1 2 3 4 5 6 7 8 9 10 11 -( void )setTitle:( NSString *)title{ [ self setTitle:title forState:UIControlStateNormal]; self .titleLabel.textAlignment= NSTextAlignmentCenter ; self .titleLabel.font = [UIFont systemFontOfSize:15]; //正常状态下是灰色 [ self setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; //不可点击的时候切换文字颜色 [ self setTitleColor:[UIColor orangeColor] forState:UIControlStateDisabled]; //设置图片属性 self .imageView.contentMode = UIViewContentModeCenter; } GPDockItem.m中的代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 // // GPDockItem.m // GrouponProject //博客园FlyElephant:http://www.cnblogs.com/xiaofeixiang // Created by keso on 15/3/11. // Copyright (c) 2015年 keso. All rights reserved. // #import "GPDockItem.h" @implementation GPDockItem /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ -(instancetype)initWithFrame:(CGRect)frame{ self =[ super initWithFrame:frame]; // if (self) { //// UIImageView *splitLine = [[UIImageView alloc] init]; //// splitLine.frame = CGRectMake(0, 0, GPDockItemWidth, 2); //// splitLine.image = [UIImage imageNamed:@"separator_tabbar_item.png"]; //// [self addSubview:splitLine]; // // } return self ; } -( void )setTitle:( NSString *)title{ [ self setTitle:title forState:UIControlStateNormal]; self .titleLabel.textAlignment= NSTextAlignmentCenter ; self .titleLabel.font = [UIFont systemFontOfSize:15]; //正常状态下是灰色 [ self setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; //不可点击的时候切换文字颜色 [ self setTitleColor:[UIColor orangeColor] forState:UIControlStateDisabled]; //设置图片属性 self .imageView.contentMode = UIViewContentModeCenter; } -( void )imageSetting:( NSString *)backgroundImage selectedImage:( NSString *)selectedImage{ self .backgroundImage=backgroundImage; self .selectedImage=selectedImage; } //设置背景图片 -( void )setBackgroundImage:( NSString *)backgroundImage{ _backgroundImage=backgroundImage; [ self setImage:[UIImage imageNamed:backgroundImage] forState:UIControlStateNormal]; } //设置选中图片 -( void )setSelectedImage:( NSString *)selectedImage{ _selectedImage=selectedImage; [ self setImage:[UIImage imageNamed:selectedImage] forState:UIControlStateDisabled]; } //设置图片区域 -(CGRect)imageRectForContentRect:(CGRect)contentRect{ CGFloat width=contentRect.size.width; CGFloat height= contentRect.size.height * 0.7; return CGRectMake(0, 10, width, height); } //设置文字区域 -(CGRect)titleRectForContentRect:(CGRect)contentRect{ CGFloat width=contentRect.size.width; CGFloat height= contentRect.size.height * 0.3; CGFloat position=contentRect.size.height*0.7; return CGRectMake(0, position, width, height); } -( void )setFrame:(CGRect)frame{ //固定Item宽高 frame.size=CGSizeMake(GPDockItemWidth, GPDockItemHeight); [ super setFrame:frame]; } @end 继承自GPDockItem的GPBottomItem,只需要设置横竖屏自动伸缩属性即可: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 // // GPBottomItem.m // GrouponProject // FlyElephant--http://www.cnblogs.com/xiaofeixiang // Created by keso on 15/3/13. // Copyright (c) 2015年 keso. All rights reserved. // #import "GPBottomItem.h" @implementation GPBottomItem /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ -(instancetype)initWithFrame:(CGRect)frame{ self =[ super initWithFrame:frame]; if ( self ) { // 自动伸缩 self .autoresizingMask=UIViewAutoresizingFlexibleTopMargin; } return self ; } @end GPDock.h中的定位: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 -( void )addLocation{ GPBottomItem *tabItem=[[GPBottomItem alloc]init]; [tabItem imageSetting:@ "Toolbar_switchcity.png" selectedImage:@ "Toolbar_switchcity_selected.png" ]; CGFloat y = self .frame.size.height - GPDockItemHeight*2-20; //设置位置 tabItem.frame = CGRectMake(0, y, 0, 0); [tabItem setTitle:@ "北京" ]; //设置选中触摸选中事件 [tabItem addTarget: self action: @selector (tabItemTouchEvent:) forControlEvents:UIControlEventTouchDown]; tabItem.tag =4; [ self addSubview:tabItem]; } GPDock.h中的设置: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 -( void )addSetting{ GPBottomItem *tabItem=[[GPBottomItem alloc]init]; [tabItem imageSetting:@ "Toolbar_setting.png" selectedImage:@ "Toolbar_setting_selected.png" ]; CGFloat y = self .frame.size.height - GPDockItemHeight-10; //设置位置 tabItem.frame = CGRectMake(0, y, 0, 0); [tabItem setTitle:@ "设置" ]; //设置选中触摸选中事件 [tabItem addTarget: self action: @selector (tabItemTouchEvent:) forControlEvents:UIControlEventTouchDown]; tabItem.tag =5; [ self addSubview:tabItem]; } 两者有相同之处,分开合并都行,具体看将来要实现的业务逻辑,将其添加到GPDock中: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 -(instancetype)initWithFrame:(CGRect)frame{ self =[ super initWithFrame:frame]; if ( self ) { //自动伸缩高度可伸缩,右边距可以伸缩 self .autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleRightMargin; //设置背景图片 self .backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@ "Toolbar_bg_tabbar.png" ]]; //添加选项卡 [ self addTabItems]; //添加设置 [ self addLocation]; //添加设置 [ self addSetting]; } return self ; } 最终实现效果如下: 本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4334242.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android开发学习笔记:Android很有用的代码片段

1:查看是否有存储卡插入 Stringstatus=Environment.getExternalStorageState(); if(status.equals(Enviroment.MEDIA_MOUNTED)){ 说明有SD卡插入 } 2:让某个Activity透明 OnCreate中不设Layout this.setTheme(R.style.Theme_Transparent); 以下是Theme_Transparent的定义(注意transparent_bg是一副透明的图片) 3:在屏幕元素中设置句柄 使用Activity.findViewById来取得屏幕上的元素的句柄.使用该句柄您可以设置或获取任何该对象外露的值. TextViewmsgTextView=(TextView)findViewById(R.id.msg); msgTextView.setText(R.string.push_me); 4:发送短信 Stringbody=”thisismmsdemo”; Intentmmsintent=newIntent(Intent.ACTION_SENDTO,Uri.fromParts(”smsto”,number,null)); mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY,body); mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE,true); mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT,true); startActivity(mmsintent); 5:发送彩信 StringBuildersb=newStringBuilder(); sb.append(”file://”); sb.append(fd.getAbsoluteFile()); Intentintent=newIntent(Intent.ACTION_SENDTO,Uri.fromParts(”mmsto”,number,null)); //Belowextradatasarealloptional. intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_SUBJECT,subject); intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY,body); intent.putExtra(Messaging.KEY_ACTION_SENDTO_CONTENT_URI,sb.toString()); intent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE,composeMode); intent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT,exitOnSent); startActivity(intent); 7:发送Mail mime=“img/jpg”; shareIntent.setDataAndType(Uri.fromFile(fd),mime); shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(fd)); shareIntent.putExtra(Intent.EXTRA_SUBJECT,subject); shareIntent.putExtra(Intent.EXTRA_TEXT,body); 8:注册一个BroadcastReceiver registerReceiver(mMasterResetReciever,newIntentFilter(”oms.action.MASTERRESET”)); privateBroadcastReceivermMasterResetReciever=newBroadcastReceiver(){ publicvoidonReceive(Contextcontext,Intentintent){ Stringaction=intent.getAction(); if(”oms.action.MASTERRESET”.equals(action)){ RecoverDefaultConfig(); } } }; 9:定义ContentObserver,监听某个数据表 privateContentObservermDownloadsObserver=newDownloadsChangeObserver(Downloads.CONTENT_URI); privateclassDownloadsChangeObserverextendsContentObserver{ publicDownloadsChangeObserver(Uriuri){ super(newHandler()); } @Override publicvoidonChange(booleanselfChange){} } 10:获得手机UA publicStringgetUserAgent(){ Stringuser_agent=ProductProperties.get(ProductProperties.USER_AGENT_KEY,null); returnuser_agent; } 11:清空手机上Cookie CookieSyncManager.createInstance(getApplicationContext()); CookieManager.getInstance().removeAllCookie(); 12:建立GPRS连接 //DialtheGPRSlink. privatebooleanopenDataConnection(){ //Setupdataconnection. DataConnectionconn=DataConnection.getInstance(); if(connectMode==0){ ret=conn.openConnection(mContext,“cmwap”,“cmwap”,“cmwap”); }else{ ret=conn.openConnection(mContext,“cmnet”,“”,“”); } } 13:PreferenceActivity用法 publicclassSettingextendsPreferenceActivity{ publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.settings); } } Setting.xml: android:key=”seting2″ android:title=”@string/seting2″ android:summary=”@string/seting2″/> android:key=”seting1″ android:title=”@string/seting1″ android:summaryOff=”@string/seting1summaryOff” android:summaryOn=”@stringseting1summaryOff”/> 14:通过HttpClient从指定server获取数据 DefaultHttpClienthttpClient=newDefaultHttpClient(); HttpGetmethod=newHttpGet(“http://www.baidu.com/1.html”); HttpResponseresp; Readerreader=null; try{ //AllClientPNames.TIMEOUT HttpParamsparams=newBasicHttpParams(); params.setIntParameter(AllClientPNames.CONNECTION_TIMEOUT,10000); httpClient.setParams(params); resp=httpClient.execute(method); intstatus=resp.getStatusLine().getStatusCode(); if(status!=HttpStatus.SC_OK)returnfalse; //HttpStatus.SC_OK; returntrue; }catch(ClientProtocolExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); }catch(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); }finally{ if(reader!=null)try{ reader.close(); }catch(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } 15:显示toast Toast.makeText(this._getApplicationContext(),R.string._item,Toast.LENGTH_SHORT).show(); 16:在当前Activity中启动另外一个Activity startActivity(newIntent(this,目标Activity.class)); 17:从当前ContentView从查找控件 (Button)findViewById(R.id.btnAbout) R.id.btnAbout指控件id。 18:获取屏幕宽高 DisplayMetricsdm=newDisplayMetrics(); //获取窗口属性 getWindowManager().getDefaultDisplay().getMetrics(dm); intscreenWidth=dm.widthPixels;//320 intscreenHeight=dm.heightPixels;//480 19:无标题栏、全屏 //无标题栏 requestWindowFeature(Window.FEATURE_NO_TITLE); //全屏模式 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 注意在setContentView()之前调用,否则无效。 20注册activity 所有用到的Activity都必须在AndroidManifest.xml中注册,否则会报空指针错误。 如:,注意是包名+类名。 会持续更新...................... 本文转自 lingdududu 51CTO博客,原文链接http://blog.51cto.com/liangruijun/722171

优秀的个人博客,低调大师

Android开发学习笔记:浅谈3大类菜单

在Android系统中,菜单可以分为三类:选项菜单(Option Menu),上下文菜单(Context Menu)以及子菜单(Sub Menu)。 一.选项菜单(Option Menu) 创建选项菜单的步骤: 1.覆盖Activity的onCreateOptionMenu(Menu menu)方法,当菜单第一次被打开时调用 2.调用Menu的add( )方法添加菜单项(MenuItem),同时可以调用MenuItem的setIcon( )方法来为菜单项设置图标 3.当菜单项(MenuItem)被选择时,覆盖Activity的onOptionsItemSelected(MenuItem item)来响应事件 选项菜单的实例: packagecom.android.menu.activity; importandroid.app.Activity; importandroid.os.Bundle; importandroid.view.Menu; importandroid.view.MenuItem; publicclassOptionMenuActivityextendsActivity{ //声明菜单项常量 privatestaticfinalintITEM_1=Menu.FIRST; privatestaticfinalintITEM_2=Menu.FIRST+1; privatestaticfinalintITEM_3=Menu.FIRST+2; publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); } //覆盖onCreateOptionsMenu(Menumenu)来添加菜单项 publicbooleanonCreateOptionsMenu(Menumenu){ //android.R使用的是系统自带的图标 menu.add(0,ITEM_1,0,"开始").setIcon(android.R.drawable.ic_media_play); menu.add(0,ITEM_2,0,"帮助").setIcon(android.R.drawable.ic_menu_help); menu.add(0,ITEM_3,0,"退出").setIcon(android.R.drawable.ic_menu_close_clear_cancel); returntrue; } //覆盖onOptionsItemSelected(MenuItemitem)来响应菜单选项被单击事件 publicbooleanonOptionsItemSelected(MenuItemitem){ switch(item.getItemId()){ caseITEM_1: setTitle("开始游戏!"); break; caseITEM_2: setTitle("查看帮助!"); break; caseITEM_3: setTitle("退出游戏!"); break; } returntrue; } } 效果图: 二.上下文菜单(Context Menu) 创建上下文菜单的步骤: 1.覆盖Activity的onCreateOptionMenu(Menu menu)方法,调用Menu的add( )方法添加菜单项(MenuItem). 2.覆盖Activity的onOptionsItemSelected(MenuItem item)来响应事件 3.调用registerForContextMenu( )方法来为视图注册上下文菜单 上下文菜单的实例(按住“测试Context Menu”2秒,就会出现上下文菜单): packagecom.android.context.activity; importandroid.app.Activity; importandroid.graphics.Color; importandroid.os.Bundle; importandroid.view.ContextMenu; importandroid.view.Menu; importandroid.view.MenuItem; importandroid.view.View; importandroid.view.ContextMenu.ContextMenuInfo; importandroid.widget.TextView; publicclassContextMenuActivityextendsActivity{ privatestaticfinalintITME_1=Menu.FIRST; privatestaticfinalintITME_2=Menu.FIRST+1; privatestaticfinalintITME_3=Menu.FIRST+2; privateTextViewtext; publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); text=(TextView)findViewById(R.id.TextView_1); //调用registerForContextMenu()来注册上下文菜单 registerForContextMenu(text); } @Override publicvoidonCreateContextMenu(ContextMenumenu,Viewv, ContextMenuInfomenuInfo){ menu.add(0,ITME_1,0,"红色背景"); menu.add(0,ITME_2,0,"蓝色背景"); menu.add(0,ITME_3,0,"白色背景"); } @Override publicbooleanonContextItemSelected(MenuItemitem){ switch(item.getItemId()){ caseITME_1: //设置TextView的背景颜色 text.setBackgroundColor(Color.RED); break; caseITME_2: text.setBackgroundColor(Color.BLUE); break; caseITME_3: text.setBackgroundColor(Color.WHITE); break; } returntrue; } } 效果图: 三.子菜单(Sub Menu) 创建子菜单的步骤: 1.覆盖Activity的onCreateOptionMenu(Menu menu)方法,调用Menu的addSubMenu( )方法来添加子菜单(Sub Menu) 2.调用SubMenu的add( )方法来添加子菜单(Sub Menu) 3.覆盖onContextItemSelect( )方法来响应菜单单击事件 子菜单的实例: packagecom.android.sub.activity; importandroid.app.Activity; importandroid.os.Bundle; importandroid.view.Menu; importandroid.view.MenuItem; importandroid.view.SubMenu; publicclassSubMenuActivityextendsActivity{ privatestaticfinalintITEM_1=Menu.FIRST; privatestaticfinalintITEM_2=Menu.FIRST+1; publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); } //覆盖onCreateOptionsMenu(Menumenu)方法,添加子菜单项 publicbooleanonCreateOptionsMenu(Menumenu){ SubMenuad=menu.addSubMenu("添加").setIcon(android.R.drawable.ic_menu_add); SubMenure=menu.addSubMenu("关闭").setIcon(android.R.drawable.ic_menu_close_clear_cancel); ad.add(0,ITEM_1,0,"文件"); ad.add(0,ITEM_2,0,"图片"); returntrue; } //覆盖onOptionsItemSelected(MenuItemitem),响应菜单选项被单击事件 publicbooleanonOptionsItemSelected(MenuItemitem){ switch(item.getItemId()){ caseITEM_1: setTitle("添加文件!"); break; caseITEM_2: setTitle("添加图片!"); break; } returntrue; } } 效果图: 注:menu.add的方法中的参数: 1.int类型的group ID参数,代表的是组概念,你可以将几个菜单项归为一组,以便更好的以组的方式管理你的菜单按钮。可以用到的方法有: removeGroup(id) setGroupCheckable(id, checkable, exclusive) setGroupEnabled(id,boolean enabled) setGroupVisible(id,visible)2.int类型的item ID参数,代表的是项目编号。这个参数非常重要,一个item ID对应一个Menu中的选项。在后面使用菜单的时候,就是靠这个item ID来判断,你选中的是哪个选项。3.int类型的order ID参数,代表的是菜单项的显示顺序。默认是0,表示菜单的显示顺序就是按照add的顺序来显示。4.String类型的title参数,表示选项中显示的文字。 本文转自 lingdududu 51CTO博客,原文链接: http://blog.51cto.com/liangruijun/641275

优秀的个人博客,低调大师

Android开发学习:使用已有的sql数据库

之前我们使用的数据库都是在代码里面创建的。下面介绍一下如果使用外部已有的sql数据库。 先用SQLite管理工具,sqliteadmin 具体操作很简单,在这里我就不详细介绍的了,但有一个地方时候很值得注意的,就是用sqliteadmin创建数据库的时候,数据库保存的路径不能是中文路径,中文路径会出现下面的错误提示: 我在sqliteadmin 创建好数据库StuDB,里面的表如下: 将创建好的数据库在DDMS中点击导入到data/data/程序的包名/ SQLiteTestActivity.java packagecom.lingdududu.test; importandroid.app.Activity; importandroid.database.Cursor; importandroid.database.sqlite.SQLiteDatabase; importandroid.os.Bundle; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; importandroid.widget.EditText; importandroid.widget.Toast; publicclassSQLiteTestActivityextendsActivity{ /**Calledwhentheactivityisfirstcreated.*/ privateEditTextstudentText; privateEditTextteacherText; privateButtonqueryBtn; SQLiteDatabasestuDb; @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); studentText=(EditText)findViewById(R.id.stu_name); teacherText=(EditText)findViewById(R.id.teacher_name); queryBtn=(Button)findViewById(R.id.query); queryBtn.setOnClickListener(newqueryListener()); } classqueryListenerimplementsOnClickListener{ @Override publicvoidonClick(Viewv){ //调用查询方法 query(); stuDb.close(); } } //查询方法 privatevoidquery(){ //打开或者创建数据库 stuDb=SQLiteDatabase.openOrCreateDatabase("data/data/com.lingdududu.test/StuDB.s3db",null); try{ Stringstring=studentText.getText().toString(); Stringsql="SelectsnamefromStudentwheresnumber="+string; Cursorcursor=stuDb.rawQuery(sql,null); cursor.moveToFirst(); teacherText.setText(cursor.getString(cursor.getColumnIndex("sname"))); }catch(Exceptione){ Toast.makeText(this,"请检查输入的学生学号是否正确",Toast.LENGTH_LONG).show(); } } } main.xml <?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/input_name" /> <EditText android:id="@+id/stu_name" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/query" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="开始查询" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/teacher_name" /> <EditText android:id="@+id/teacher_name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:editable="false" /> </LinearLayout> strings.xml <?xmlversion="1.0"encoding="utf-8"?> <resources> <stringname="hello">HelloWorld,SQLiteTestActivity!</string> <stringname="app_name">SQLiteTest</string> <stringname="input_name">请输入学生学号</string> <stringname="teacher_name">该学生的姓名</string> </resources> 效果图: 本文转自 lingdududu 51CTO博客,原文链接: http://blog.51cto.com/liangruijun/728272

优秀的个人博客,低调大师

Android开发学习笔记:Button事件实现方法的总结

下面介绍Button事件实现的两种方法 main.xml <?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/myButton1" android:text="按钮1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/myButton2" android:text="按钮2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> strings.xml <?xmlversion="1.0"encoding="utf-8"?> <resources> <stringname="hello">HelloWorld,ButtonDemoActivity!</string> <stringname="app_name">ButtonDemo</string> </resources> 第一种: ButtonDemoActivity.java packagecom.android.ButtonDemo.activity; importandroid.app.Activity; importandroid.os.Bundle; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; importandroid.widget.Toast; publicclassButtonDemoActivityextendsActivity{ ButtonmyButton1,myButton2; @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); myButton1=(Button)findViewById(R.id.myButton1); myButton2=(Button)findViewById(R.id.myButton2); //使用匿名类注册Button事件 myButton1.setOnClickListener(newOnClickListener() { publicvoidonClick(Viewv) { Toast.makeText(ButtonDemoActivity.this,"你点击了按钮1",Toast.LENGTH_LONG).show(); } }); myButton2.setOnClickListener(newOnClickListener() { publicvoidonClick(Viewv) { Toast.makeText(ButtonDemoActivity.this,"你点击了按钮2",Toast.LENGTH_LONG).show(); } }); } } 第二种: ButtonDemoActivity.java packagecom.android.ButtonDemo.activity; importandroid.app.Activity; importandroid.os.Bundle; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; importandroid.widget.Toast; publicclassButtonDemoActivityextendsActivity{ ButtonmyButton1,myButton2; @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); myButton1=(Button)findViewById(R.id.myButton1); myButton2=(Button)findViewById(R.id.myButton2); myButton1.setOnClickListener(newButtonClick()); myButton2.setOnClickListener(newButtonClick()); } //创建一个类,来响应OnClickListener classButtonClickimplementsOnClickListener { publicvoidonClick(Viewv) { switch(v.getId()){ caseR.id.myButton1: Toast.makeText(ButtonDemoActivity.this,"你点击了按钮1",Toast.LENGTH_LONG).show(); break; caseR.id.myButton2: Toast.makeText(ButtonDemoActivity.this,"你点击了按钮2",Toast.LENGTH_LONG).show(); break; default: break; } } } } 本文转自 lingdududu 51CTO博客,原文链接: http://blog.51cto.com/liangruijun/629329

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册