首页 文章 精选 留言 我的

精选列表

搜索[网站开发],共10000篇文章
优秀的个人博客,低调大师

Android Studio第三十三期 - 蓝牙开发初识~

遇见到坑爹的队友只有接受现实并且解决问题~ 首先介绍一下网上几乎所有的能搜到的方法: 1.首先,要操作蓝牙,先要在AndroidManifest.xml里加入权限 1 2 <uses-permissionandroid:name= "android.permission.BLUETOOTH_ADMIN" /> <uses-permissionandroid:name= "android.permission.BLUETOOTH" /> 2.在android.bluetooth包下就这些能用的东西: 3.我们一般用的是BluetoothAdapter基本就可以实现需求了: cancelDiscovery() :取消发现,也就是说当我们正在搜索设备的时候调用这个方法将不再继续搜索 disable():关闭蓝牙 enable():打开蓝牙,这个方法打开蓝牙不会弹出提示,更多的时候我们需要问下用户是否打开,一下这两行代码同样是打开蓝牙,不过会提示用户: 1 2 3 4 5 6 7 8 9 //打开蓝牙 if (!mBluetoothAdapter.isEnabled()){ Intentintent1= new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent1,ENABLE_BLUE); //不做提示,强行打开 //mAdapter.enable(); } else { close_blue(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 @Override protected void onActivityResult( int requestCode, int resultCode,Intentdata){ //TODOAuto-generatedmethodstub super .onActivityResult(requestCode,resultCode,data); if (requestCode==ENABLE_BLUE){ if (resultCode==RESULT_OK){ Toast.makeText( this , "蓝牙开启成功" ,Toast.LENGTH_SHORT).show(); open_blue(); } else if (resultCode==RESULT_CANCELED){ Toast.makeText( this , "蓝牙开始失败" ,Toast.LENGTH_SHORT).show(); close_blue(); } } } getAddress():获取本地蓝牙地址 getDefaultAdapter():获取默认BluetoothAdapter,实际上,也只有这一种方法获取BluetoothAdapter getName():获取本地蓝牙名称 getRemoteDevice(String address):根据蓝牙地址获取远程蓝牙设备 getState():获取本地蓝牙适配器当前状态(感觉可能调试的时候更需要) isDiscovering():判断当前是否正在查找设备,是返回true isEnabled():判断蓝牙是否打开,已打开返回true,否则,返回false listenUsingRfcommWithServiceRecord(String name,UUID uuid):根据名称,UUID创建并返回BluetoothServerSocket,这是创建BluetoothSocket服务器端的第一步 startDiscovery():开始搜索,这是搜索的第一步 4.使设备能够被搜索: 首先判断初始状态: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 //本机设备被扫描初始状态bufen if (mBluetoothAdapter.getScanMode()== 23 ){ //开启状态设置bufen //开启操作 //弹窗 //Intentintent=newIntent( //BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); ////设置蓝牙可见性的时间,方法本身规定最多可见300秒 //intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,BLUE_TIME); //startActivityForResult(intent,SEARCH_BLUE); //不弹窗 SettingblueUtils.setDiscoverableTimeout( 120 ); shezhi_blue_research( true ); new Handler().postDelayed( new Runnable(){ @Override public void run(){ closeDiscoverableTimeout(); shezhi_blue_research( false ); } }, 120 * 1000 ); } else if (mBluetoothAdapter.getScanMode()== 21 ){ //关闭状态设置bufen closeDiscoverableTimeout(); } 然后设置代码部分: 开启: 1 2 3 4 5 6 7 8 9 10 11 12 13 public static void setDiscoverableTimeout( int timeout){ BluetoothAdapteradapter=BluetoothAdapter.getDefaultAdapter(); try { MethodsetDiscoverableTimeout=BluetoothAdapter. class .getMethod( "setDiscoverableTimeout" , int . class ); setDiscoverableTimeout.setAccessible( true ); MethodsetScanMode=BluetoothAdapter. class .getMethod( "setScanMode" , int . class , int . class ); setScanMode.setAccessible( true ); setDiscoverableTimeout.invoke(adapter,timeout); setScanMode.invoke(adapter,BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE,timeout); } catch (Exceptione){ e.printStackTrace(); } } 关闭: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public static void closeDiscoverableTimeout(){ BluetoothAdapteradapter=BluetoothAdapter.getDefaultAdapter(); try { MethodsetDiscoverableTimeout=BluetoothAdapter. class .getMethod( "setDiscoverableTimeout" , int . class ); setDiscoverableTimeout.setAccessible( true ); MethodsetScanMode=BluetoothAdapter. class .getMethod( "setScanMode" , int . class , int . class ); setScanMode.setAccessible( true ); setDiscoverableTimeout.invoke(adapter, 1 ); setScanMode.invoke(adapter,BluetoothAdapter.SCAN_MODE_CONNECTABLE, 1 ); } catch (Exceptione){ e.printStackTrace(); } } 5.抓取蓝牙列表List的方法: 1 2 3 4 5 6 //获取所有已经绑定的蓝牙设备 Set<BluetoothDevice>devices=mBluetoothAdapter.getBondedDevices(); List<BluetoothDevice>mList1= new ArrayList<>(); if (devices.size()> 0 ){ mList1= new ArrayList<>(devices); } 6.注册搜索蓝牙receiver: 1 2 3 4 5 6 IntentFiltermFilter= new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); mFilter.addAction(BluetoothDevice.ACTION_FOUND); mFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); mFilter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); mFilter.addAction(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED); registerReceiver(mReceiver,mFilter); 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 private BroadcastReceivermReceiver= new BroadcastReceiver(){ @Override public void onReceive(Contextcontext,Intentintent){ //TODOAuto-generatedmethodstub Stringaction=intent.getAction(); //获得已经搜索到的蓝牙设备 if (action.equals(BluetoothDevice.ACTION_FOUND)){ BluetoothDevicedevice11=intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); //搜索到的不是已经绑定的蓝牙设备 if (!is_shebei_open){ receiver1(device11); } else { receiver11(device11); } /**当绑定的状态改变时*/ } else if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)){ BluetoothDevicedevice22=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); receiver2(device22,MainActivity_BlueTooth. this ); } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){ receiver3(); } else if (action.equals(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED)){ int state=intent.getIntExtra(BluetoothA2dp.EXTRA_STATE,BluetoothA2dp.STATE_DISCONNECTED); receiver4(state); } else if (action.equals(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED)){ int state=intent.getIntExtra(BluetoothA2dp.EXTRA_STATE,BluetoothA2dp.STATE_NOT_PLAYING); receiver5(state); } } }; 最后说一下,多连接目前还没有解决,个人试了很多方法,大家如果有方法告诉我,谢谢~ 本文转自 吴雨声 51CTO博客,原文链接:http://blog.51cto.com/liangxiao/1905941,如需转载请自行联系原作者

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

【移动开发】Android中Fragment+ViewPager的配合使用

原本在上一篇博客中要讲解一个Fragment的综合应用,但是中间又想到了滑屏方式,所以就分类在总结了一下,(http://smallwoniu.blog.51cto.com/3911954/1308959)今天我将继续总结,关于Fragment+ViewPager的使用! 官方文档:http://developer.android.com/reference/android/support/v4/view/ViewPager.html ViewPager is most often used in conjunction withFragment, which is a convenient way to supply and manage the lifecycle of each page. There are standard adapters implemented for using fragments with the ViewPager, which cover the most common use cases. These areFragmentPagerAdapterandFragmentStatePagerAdapter; each of these classes have simplecode showing how to build a full user interface with them. 这里大家可以回忆一下如果像上篇中介绍ViewPager的使用,叶片填充数据是Layout,页面少的话还可以,如果页面过多的话,全部加载到手机内存中,可能会耗尽内存,手动销毁又太麻烦。官方推荐ViewPager与Fragment一起使用,可以更加方便的管理每个Page的生命周期,这里有标准的适配器实现用于ViewPager和Fragment,涵盖最常见的用例。FragmentPagerAdapter和FragmentStatePagerAdapter这两个类都有简单的代码显示如何构建一个完整的用户界面与他们。 适配器类: 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 package com.zhf.android_viewpager_fragment; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; /** * 自定义fragment适配器 * @author ZHF * */ public class MyFragmentPageAdapter extends FragmentPagerAdapter { public MyFragmentPageAdapter(FragmentManager fm) { super (fm); } @Override public int getCount() { return 3 ; } @Override public Fragment getItem( int position) { switch (position) { case 0 : return MyFragment.newInstance(position); case 1 : return MyFragment.newInstance(position); case 2 : return MyFragment.newInstance(position); default : return null ; } } } MyFragment类: 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 package com.zhf.android_viewpager_fragment; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * 用于创建Fragment对象,作为ViewPager的叶片 * @author ZHF * */ public class MyFragment extends Fragment { int mNum; //页号 public static MyFragment newInstance( int num) { MyFragment fragment = new MyFragment(); // Supply num input as an argument. Bundle args = new Bundle(); args.putInt( "num" , num); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); //这里我只是简单的用num区别标签,其实具体应用中可以使用真实的fragment对象来作为叶片 mNum = getArguments() != null ? getArguments().getInt( "num" ) : 1 ; } /**为Fragment加载布局时调用**/ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_pager_list, null ); TextView tv = (TextView) view.findViewById(R.id.text); tv.setText( "fragment+" + mNum); return view; } } 布局文件: activity_main.xml 1 2 3 4 5 6 7 8 9 10 <RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" > <android.support.v4.view.ViewPager android:id= "@+id/viewpager" android:layout_width= "fill_parent" android:layout_height= "fill_parent" /> </RelativeLayout> fragment_pager_list.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:background= "@android:drawable/gallery_thumb" android:orientation= "vertical" > <TextView android:id= "@+id/text" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:gravity= "center_vertical|center_horizontal" android:text= "@string/hello_world" android:textAppearance= "?android:attr/textAppearanceMedium" /> </LinearLayout> MainActivity类: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com.zhf.android_viewpager_fragment; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.view.ViewPager; public class MainActivity extends FragmentActivity { private ViewPager mViewPager; private MyFragmentPageAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); mViewPager = (ViewPager) findViewById(R.id.viewpager); //这里因为是3.0一下版本,所以需继承FragmentActivity,通过getSupportFragmentManager()获取FragmentManager //3.0及其以上版本,只需继承Activity,通过getFragmentManager获取事物 FragmentManager fm = getSupportFragmentManager(); //初始化自定义适配器 mAdapter = new MyFragmentPageAdapter(fm); //绑定自定义适配器 mViewPager.setAdapter(mAdapter); } } 效果图: 效果与ViewPager中添加View的效果是一样的!但是它与View的区别在于它有自己的生命周期,可以随时更改自己的状态便于管理。 事实上使用FragmentPagerAdapter时,Fragment对象会一直存留在内存中,所以当有大量的显示页时,就不适合用FragmentPagerAdapter了,FragmentPagerAdapter适用于只有少数的page情况,像选项卡。 这个时候你可以考虑使用FragmentStatePagerAdapter,当使用FragmentStatePagerAdapter时,如果Fragment不显示,那么Fragment对象会被销毁,(滑过后会保存当前界面,以及下一个界面和上一个界面(如果有),最多保存3个,其他会被销毁掉)但在回调onDestroy()方法之前会回调onSaveInstanceState(Bundle outState)方法来保存Fragment的状态,下次Fragment显示时通过onCreate(Bundle savedInstanceState)把存储的状态值取出来,FragmentStatePagerAdapter比较适合页面比较多的情况,像一个页面的ListView。 本文转自zhf651555765 51CTO博客,原文链接:http://blog.51cto.com/smallwoniu/1322746,如需转载请自行联系原作者

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

Android UI开发第九篇——SlidingDrawer 抽屉效果

SlidingDrawer是自SDK 1.5才新加入的,实现Launcher的抽屉效果。SlidingDrawer配置上采用了水平展开或垂直展开两种(android:orientation)方式,在XML里必须指定其使用的android:handle与android:content,前者委托要展开的图片(Layout配置),后者则是要展开的Layout Content。 <?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" android:background="@drawable/i1" > <SlidingDrawer android:id="@+id/slidingdrawer" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:handle="@+id/handle" android:content="@+id/content"> <Button android:id="@+id/handle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/ic_dialog_dialer" /> <LinearLayout android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/default_bg"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <EditText android:id="@+id/editText" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> </SlidingDrawer> </LinearLayout> 补充: 一、简介 SlidingDrawer隐藏屏外的内容,并允许用户通过handle以显示隐藏内容。它可以垂直或水平滑动,它有俩个View组成,其一是可以拖动的handle,其二是隐藏内容的View.它里面的控件必须设置布局,在布局文件中必须指定handle和content. 例如下面 <SlidingDrawer android:layout_width="fill_parent" android:layout_height="fill_parent" android:handle="@+id/handle" android:content="@+id/content" android:orientation="vertical" android:id="@+id/slidingdrawer"> <ImageButton android:id="@id/handle" android:layout_width="50dip" android:layout_height="44dip" android:src="@drawable/up" /> <LinearLayout android:id="@id/content" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff"> <TextView android:text="这是一个滑动式抽屉的示例" android:id="@+id/tv" android:textSize="18px" android:textColor="#000000" android:gravity="center_vertical|center_horizontal" android:layout_width="match_parent" android:textStyle="bold" android:layout_height="match_parent"></TextView> </LinearLayout> </SlidingDrawer> 二、重要属性 android:allowSingleTap:指示是否可以通过handle打开或关闭 android:animateOnClick:指示是否当使用者按下手柄打开/关闭时是否该有一个动画。 android:content:隐藏的内容 android:handle:handle(手柄) 三、重要方法 animateClose():关闭时实现动画。 close():即时关闭 getContent():获取内容 isMoving():指示SlidingDrawer是否在移动。 isOpened():指示SlidingDrawer是否已全部打开 lock():屏蔽触摸事件。 setOnDrawerCloseListener(SlidingDrawer.OnDrawerCloseListener onDrawerCloseListener):SlidingDrawer关闭时调用 unlock():解除屏蔽触摸事件。 toggle():切换打开和关闭的抽屉SlidingDrawer。 四、完整实例 1.布局文件slidingdrawer.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" android:background="@drawable/default_bg"> <SlidingDrawer android:layout_width="fill_parent" android:layout_height="fill_parent" android:handle="@+id/handle" android:content="@+id/content" android:orientation="vertical" android:id="@+id/slidingdrawer"> <ImageButton android:id="@id/handle" android:layout_width="50dip" android:layout_height="44dip" android:src="@drawable/up" /> <LinearLayout android:id="@id/content" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff"> <TextView android:text="这是一个滑动式抽屉的示例" android:id="@+id/tv" android:textSize="18px" android:textColor="#000000" android:gravity="center_vertical|center_horizontal" android:layout_width="match_parent" android:textStyle="bold" android:layout_height="match_parent"></TextView> </LinearLayout> </SlidingDrawer> </LinearLayout> 2.Java代码 package com.wjq; import android.app.Activity; import android.os.Bundle; import android.widget.ImageButton; import android.widget.SlidingDrawer; import android.widget.TextView; public class SlidingDrawerDemo extends Activity { private SlidingDrawer mDrawer; private ImageButton imbg; private Boolean flag=false; private TextView tv; /* (non-Javadoc) * @see android.app.Activity#onCreate(android.os.Bundle) */ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.sildingdrawer); imbg=(ImageButton)findViewById(R.id.handle); mDrawer=(SlidingDrawer)findViewById(R.id.slidingdrawer); tv=(TextView)findViewById(R.id.tv); mDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() { @Override public void onDrawerOpened() { flag=true; imbg.setImageResource(R.drawable.down); } }); mDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener(){ @Override public void onDrawerClosed() { flag=false; imbg.setImageResource(R.drawable.up); } }); mDrawer.setOnDrawerScrollListener(new SlidingDrawer.OnDrawerScrollListener(){ @Override public void onScrollEnded() { tv.setText("结束拖动"); } @Override public void onScrollStarted() { tv.setText("开始拖动"); } }); } } 本文转自xyz_lmn51CTO博客,原文链接:http://blog.51cto.com/xyzlmn/817346,如需转载请自行联系原作者

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

Android UI开发第十七篇——Android Fragment实例

TitlesFragment TitlesFragment继承自Fragment的子类ListFragment,使用了一个静态数组填充列表,重写了onListItemClick方法,showDetails方法展示ListView item的详情。 DetailsFragment df = DetailsFragment.newInstance(index);//获取详情Fragment的实例 FragmentTransaction ft = getFragmentManager().beginTransaction();//获取FragmentTransaction 实例 ft.replace(R.id.details, df); //使用DetailsFragment 的实例 ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); ft.commit();//提交 这里就使用到了Android Fragment使用中介绍的第二种加载fragment的方法。看一下DetailsFragment : 布局文件中使用了fragment标签和FrameLayout标签。Android Fragment使用中介绍了2中嵌入Fragment的方法,这个实例中都用到,从布局文件看到有了fragment标签,这是一种使用方法,FrameLayout标签将会成为第二种加载fragment的载体view。 看一下程序实现(com.fragment.main.TitlesFragment): 效果图的左边是一个列表,右边是列表item的详情。 先看一下布局文件(layout): Fragment是Android honeycomb 3.0新增的概念,在Android——Fragment介绍、Android Fragment使用、Android FragmentManage FragmentTransaction介绍中做了关于Fragment的详细介绍。这一片主要通过一个实例了解Fragment的使用。 先看下实例效果图: 本文转自xyz_lmn51CTO博客,原文链接:http://blog.51cto.com/xyzlmn/817249,如需转载请自行联系原作者

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

iphone开发之C++和Objective-C混编

C++和Objective-C混编(官方文档翻译) 原文网址: http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocCPlusPlus.html Using C++ With Objective-C 苹果的Objective-C编译器允许用户在同一个源文件里自由地混合使用C++和Objective-C,混编后的语言叫Objective-C++。有了它,你就可以在Objective-C应用程序中使用已有的C++类库。 Objective-C和C++混编的要点 在Objective-C++中,可以用C++代码调用方法也可以从Objective-C调用方法。在这两种语言里对象都是指针,可以在任何地方使用。例如,C++类可以使用Objective-C对象的指针作为数据成员,Objective-C类也可以有C++对象指针做实例变量。下例说明了这一点。 注意:Xcode需要源文件以".mm"为扩展名,这样才能启动编译器的Objective-C++扩展。 /* Hello.mm * Compile with: g++ -x objective-c++ -framework Foundation Hello.mm -o hello */ #import <Foundation/Foundation.h> class Hello { private: id greeting_text; // holds an NSString public: Hello() { greeting_text = @"Hello, world!"; } Hello(const char* initial_greeting_text) { greeting_text = [[NSString alloc] initWithUTF8String:initial_greeting_text]; } void say_hello() { printf("%s\n", [greeting_text UTF8String]); } }; @interface Greeting : NSObject { @private Hello *hello; } - (id)init; - (void)dealloc; - (void)sayGreeting; - (void)sayGreeting:(Hello*)greeting; @end @implementation Greeting - (id)init { if (self = [super init]) { hello = new Hello(); } return self; } - (void)dealloc { delete hello; [super dealloc]; } - (void)sayGreeting { hello-&gt;say_hello(); } - (void)sayGreeting:(Hello*)greeting { greeting-&gt;say_hello(); } @end int main() { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; Greeting *greeting = [[Greeting alloc] init]; [greeting sayGreeting]; // &gt; Hello, world! Hello *hello = new Hello("Bonjour, monde!"); [greeting sayGreeting:hello]; // &gt; Bonjour, monde! delete hello; [greeting release]; [pool release]; return 0; } 正如你可以在OC接口中声明C结构一样,你也可以在OC接口中声明C++类。跟C结构一样,OC接口中定义的C++类是全局范围的,不是OC类的内嵌类(这与标准C(尽管不是C++)提升嵌套结构定义为文件范围是一致的)。 为了允许你基于语言变种条件化地编写代码,OC++编译器定义了__cplusplus和__OBJC__预处理器常量,分别指定C++和OC。如前所述,OC++不允许C++类继承自OC对象,也不允许OC类继承自C++对象。 class Base { /* ... */ }; @interface ObjCClass: Base ... @end // ERROR! class Derived: public ObjCClass ... // ERROR! 与OC不同的是,C++对象是静态类型的,有运行时多态是特殊情况。两种语言的对象模型因此不能直接兼容。更根本的,OC和C++对象在内存中的布局是互不相容的,也就是说,一般不可能创建一个对象实例从两种语言的角度来看都是有效的。因此,两种类型层次结构不能被混合。 你可以在OC类内部声明C++类,编译器把这些类当作已声明在全局名称空间来对待。就像下面: @interface Foo { class Bar { ... } // OK } @end Bar *barPtr; // OK OC允许C结构作为实例变量,不管它是否声明在OC声明内部。 @interface Foo { struct CStruct { ... }; struct CStruct bigIvar; // OK } ... @end Mac OS X 10.4以后,如果你设置fobjc- call-cxx-cdtors编译器标志,你就可以使用包含虚函数和有意义的用户自定义零参数构造函数、析构函数的C++类实例来做为实例变量(gcc-4.2默认设置编译器标志fobjc-call-cpp-cdtors)。OC成员变量alloc完以后,alloc函数会按声明顺序调用构造器。构造器使用公共无参数恰当的构造函数。OC成员变量dealloc之前,dealloc方法按声明顺序反序调用调用析构函数。 OC没有名称空间得概念。不能在C++名称空间内部声明OC类,也不能在OC类里声明名称空间。 OC类,协议,分类不能声明在C++ template里,C++ template也不能声明在OC接口,协议,分类的范围内。 但是,OC类可以做C++ template的参数,C++ template参数也可以做OC消息表达式的接收者或参数(不能通过selector)。 C++词汇歧义和冲突 OC头文件中定义了一些标识符,所有的OC程序必须包含的,这些标识符识id,Class,SEL,IMP和BOOL。 OC方法内,编译器预声明了标识符self和super,就想C++中的关键字this。跟C++的this不同的是,self和super是上下文相关的;OC方法外他们还可以用于普通标识符。 协议内方法的参数列表,有5个上下文相关的关键字(oneway,in,out,inout,bycopy)。这些在其他内容中不是关键字。 从OC程序员的角度来看,C++增加了不少新的关键字。你仍然可以使用C++的关键字做OC selector的一部分,所以影响并不严重,但你不能使用他们命名OC类和实例变量。例如,尽管class是C++的关键字,但是你仍然能够使用NSObject的方法class: [foo class]; // OK 然而,因为它是一个关键字,你不能用class做变量名称: NSObject *class; // Error OC里类名和分类名有单独的命名空间。@interface foo和@interface(foo)能够同时存在在一个源代码中。OC++里,你也能用C++中的类名或结构名来命名你的分类。 协议和template标识符使用语法相同但目的不同: id<someProtocolName> foo;TemplateType<SomeTypeName> bar; 为了避免这种含糊之处,编译器不允许把id做template名称。最后,C++有一个语法歧义,当一个label后面跟了一个表达式表示一个全局名称时,就像下面: label: ::global_name = 3; 第一个冒号后面需要空格。OC++有类似情况,也需要一个空格: receiver selector: ::global_c++_name; 限制 OC++没有为OC类增加C++的功能,也没有为C++类增加OC的功能。例如,你不能用OC语法调用C++对象,也不能为OC对象增加构造函数和析构函数,也不能将this和self互相替换使用。类的体系结构是独立的。C++类不能继承OC类,OC类也不能继承C++类。另外,多语言异常处理是不支持的。也就是说,一个OC抛出的异常不能被C++代码捕获,反过来C++代码抛出的异常不能被OC代码捕获。 本文转自 arthurchen 51CTO博客,原文链接:http://blog.51cto.com/arthurchen/577941,如需转载请自行联系原作者

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

Android开发学习:在Eclipse中导入Android项目方法

在Eclipse中导入Android项目方法的具体步骤如下: 1.启动Eclipse,依次选择File---Import,如下图所示: 2.在弹出的Import窗口中选择Existing Projects into Workspace,然后单击Next按钮,如下图所示: 3.在弹出的Import窗口选择Browse按钮,选择要导入项目的位置,如下图所示: 4.单击Finish按钮后,完成整个导入过程,在eclipse中讲显示这个导入的实例,如下如所示 5.需要运行这个项目,依次选择Run---Run Configurations 6.在弹出的窗口中设置标志名,再选择Browse文件功能和项目位置 7.单击Run后开始运行这个项目 本文转自 lingdududu 51CTO博客,原文链接:http://blog.51cto.com/liangruijun/667705

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

Android UI开发第十三篇——android-viewflow

在论坛里,经常看到有人问如何实现UC或墨迹天气那样的拖动效果。其实大部分的实现都是参考了Launcher里的Workspace这个类。刚好看到有个开源项目也是实现了这种功能,地址在http://code.google.com/p/andro-views/,希望对有需要的人有所帮助,做出很cool的应用出来。 View Flow for Android ViewFlow is an Android UI widget providing a horizontally scrollableViewGroupwith items populated from anAdapter. Scroll down to the bottom of the page for a screen shot. The component is aLibrary Project. This means that there's no need to copy-paste resources into your own project, simply add the viewflow component as a reference to any project. Usage In your layout <org.taptwo.android.widget.ViewFlow android:id="@+id/viewflow" app:sidebuffer="5" /> The use ofapp:sidebufferis optional. It defines the number of Views to buffer on each side of the currently shown View. The default sidebuffer is 3, making up a grand total of 7 (3 * 2 + 1) Views loaded at a time (at max). To be able to use the more convenientapp:sidebufferattribute, the application namespace must be included in the same manner as the android namespace is. Please refer to the layout main.xml in the example project for a full example. Again, note that it's the application namespace andnotthe viewflow namespace that must be referred likexmlns:app="http://schemas.android.com/apk/res/your.application.package.here". In your activity ViewFlow viewFlow = (ViewFlow) findViewById(R.id.viewflow); viewFlow.setAdapter(myAdapter); Setting a different initial position (0 being default) is as easy as: viewFlow.setAdapter(myAdapter, 8); Although possible, you should not callsetSelection(...)immediately after callingsetAdapter(myAdapter)as that might load unnecessary views giving you a decrease in performance. Listen on screen change events If you need to listen to screen change events you would want to implement your ownViewFlow.ViewSwitchListenerand pass it to thesetOnViewSwitchListener()method. viewFlow.setOnViewSwitchListener(new ViewSwitchListener() { public void onSwitched(View v, int position) { // Your code here } }); Flow Indicator It is also possible to add a flow view indicator to your layout. The purpose of aFlowIndicatoris to present a visual representation of where in the item list focus is at. You may either implement aFlowIndicatoryourself or use an implementation provided by the View Flow library. The View Flow library currently supports the following indicators: Circle Flow Indicator This indicator shows a circle for eachViewin the adapter with a special circle representing the currently selected view (see screenshot below). <org.taptwo.android.widget.CircleFlowIndicator android:padding="10dip" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/viewflowindic" android:background="#00000000"/> And then you'll need to connect yourViewFlowwith theFlowIndicator: CircleFlowIndicator indic = (CircleFlowIndicator) findViewById(R.id.viewflowindic); viewFlow.setFlowIndicator(indic); The following attributes are supported:activeColor,inactiveColor,activeType(either fill or stroke),inactiveType(either fill or stroke),fadeOut(time in ms until indicator fades out, 0 = never),radius. Title Flow Indicator This indicator presents the title of the previous, current and nextViewin the adapter (see screenshot below). <org.taptwo.android.widget.TitleFlowIndicator android:id="@+id/viewflowindic" android:layout_height="wrap_content" android:layout_width="fill_parent" app:footerLineHeight="2dp" app:footerTriangleHeight="10dp" app:textColor="#FFFFFFFF" app:selectedColor="#FFFFC445" app:footerColor="#FFFFC445" app:titlePadding="10dp" app:textSize="11dp" app:selectedSize="12dp" android:layout_marginTop="10dip" app:clipPadding="5dp" /> And then you'll need to connect yourViewFlowwith theFlowIndicator: TitleFlowIndicator indicator = (TitleFlowIndicator) findViewById(R.id.viewflowindic); indicator.setTitleProvider(myTitleProvider); viewFlow.setFlowIndicator(indicator); Building a jar file If you rather want a jar file instead of a including the project as an android library, runant jarin theandroid-viewflow/viewflowfolder, to build a jar file. Caveats The manifest states a min sdk version of 4, which is true. But in any case you want to support an api level < 8 you will have to forward anonConfigurationChangedevent to theViewFlowfrom yourActivity. I know this isn't a very nice solution, feel free to propose better ones! @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); viewFlow.onConfigurationChanged(newConfig); } Contributions The following persons deserves a mention for their contributions: Eric Taix Marc Reichelt,http://marcreichelt.blogspot.com/ Want to contribute? GitHub has some great articles onhow to get started with Git and GitHuband how tofork a project. Contributers are recommended to fork the app on GitHub (but don't have too). Create a feature branch, push the branch to git hub, press Pull Request and write a simple explanation. One fix per commit. If let's say a commit closes the open issue 12. Just addcloses #12in your commit message to close that issue automagically. If you still feel uncomfortable contributing the project github-wise, don't hesistate to send a regular patch. All code that is contributed must be compliant withApache License 2.0. License Copyright (c) 2011Patrik Åkerfeldt Licensed under theApache License, Version 2.0 本文转自xyz_lmn51CTO博客,原文链接:http://blog.51cto.com/xyzlmn/817285,如需转载请自行联系原作者

资源下载

更多资源
Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

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文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册