这一篇文章专门整理一下研究过的Android面试题,内容会随着学习不断的增加,如果答案有错误,希望大家可以指正
Intent intent = new Intent(this,OtherActivity.class); startActivity(intent);显式意图还有另外一种形式
Intent intent = new Intent(); ComponentName component = new ComponentName(this, OtherActivity.class); intent.setComponent(component); startActivity(intent);其实这两种形式其实是一样的,我们看一下Intent构造函数的代码
public Intent(Context packageContext, Class<?> cls) { mComponent = new ComponentName(packageContext, cls); }这样我们就一目了然了,其实我们经常使用的Intent的构造方法是第二种方式的简化版
Intent intent = new Intent(); intent.setAction("other"); startActivity(intent);隐式意图是通过setAction来进行区分到底跳转到哪一个界面,那么我们肯定要在需要跳转的页面设置一标志,我们需要在AndroidManifest.xml中对这个进行设置
<activity android:name="com.example.lifecicledemo.OtherActivity" > <intent-filter> <action android:name="other" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher); img.setImageDrawable(drawable);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); img.setImageBitmap(bitmap);
AssetManager assetManager = getResources().getAssets(); try { InputStream is = assetManager.open("ic_launcher.png"); Bitmap bitmap = BitmapFactory.decodeStream(is); img.setImageBitmap(bitmap); } catch (IOException e) { e.printStackTrace(); }
AssetManager assetManager = getResources().getAssets(); try { InputStream is = assetManager.open("ic_launcher.png"); Drawable drawable = Drawable.createFromStream(is, null); img.setImageDrawable(drawable); } catch (IOException e) { e.printStackTrace(); }