Android软件开发中的经验总结

1. 如何让APP不休眠

在需要不休眠的Activity的onCreate方法中,在setContentView()方法前加入以下语句:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, 
		WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

例如:

protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	//加入以下语句
	getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, 
			WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	
	setContentView(R.layout.activity_main);
}

2. 如何让应用显示强制方向

在AndroidMainifest.xml文件中,在对应的Activity标签下加入如下属性:

android:screenOrientation="portrait" //强制竖屏
android:screenOrientation="landscape" //强制横屏

3. 使用RadioGroup出现非互斥的情况

在使用RadioGroup构建的单选按键组中,通过对其中的某个RadioButton设置android:checked=“true”,如下所示:

<RadioGroup
        android:id="@+id/sex_radio_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/man" 
            android:checked="true"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/woman" />
</RadioGroup>

各个单选按键理论上是互斥的(只有一个能选中),但上述的方式会出现两个都能选中的情况。解决方法对每个RadioButton都设置id属性

<RadioGroup
        android:id="@+id/sex_radio_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/man" 
            android:checked="true"
            android:id="@+id/man_radio_button"/>	//设置id

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/woman"
            android:id="@+id/woman_radio_button" />	//设置ID
</RadioGroup>

4. 对于一个未显示的View如何保存成Bitmap图

由于需要保存的View还没显示,所以需要设置View的大小,整个过程如getBitmapFromView()方法所示:

private Bitmap getBitmapFromView(View v) {
	if (v == null) {
	  return null;
	}
	int measuredWidth = View.MeasureSpec.makeMeasureSpec(pictureWidth, View.MeasureSpec.EXACTLY);
	int measuredHeight = View.MeasureSpec.makeMeasureSpec(pictureHeight, View.MeasureSpec.EXACTLY);
	v.measure(measuredWidth, measuredHeight);
	int getMeasureWidth=v.getMeasuredWidth();
	int getMeasureHeight=v.getMeasuredHeight();
	//设置View的大小
	v.layout(0, 0, getMeasureWidth, getMeasureHeight);
	
	v.setDrawingCacheEnabled(true);
	v.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
	v.setDrawingCacheBackgroundColor(Color.WHITE);
	Bitmap bmp;
	//设置Bitmap对象的大小
	bmp = Bitmap.createBitmap(getMeasureWidth, getMeasureHeight, Bitmap.Config.RGB_565);
	Canvas c = new Canvas(bmp);
	
	v.draw(c);
	return bmp;
}

5. Bitmap生成图片

将Bitmap对象保存成图片的方法:

Bitmap cachebmp;
...//获取Bitmap对象
//三个参数分别为:格式(PNG、JPG等)、质量(1-100),指定文件的输出流
//返回值为boolean类型
boolean state=cachebmp.compress(Bitmap.CompressFormat.PNG, 90, fos);

6. AlertDialog显示时,点击空白处不消失

要使AlertDialog在显示时,不会因为点击阴影部分消失,需要采用反射的方法修改其中的“mShowing”属性,将其修改为false即可。代码如下:

AlertDialog alertDialog;
.....//alertDialog对象的获取
try {
	Field field = alertDialog.getClass().getSuperclass().getDeclaredField("mShowing");
	field.setAccessible(true);
	field.set(alertDialog, false);
} catch (Exception e) {
	e.printStackTrace();
}

7. 保存文件到U盘时数据出错或为0k字节问题

这种现象经常出现在直接插拔U盘的情况下。这是由于通过FileWriter等方式写入时,数据只在缓存中,还没有真正写入到flash,此时直接拔出U盘,导致数据写入失败。
解决方法是通过FileDescriptor对象进行同步,代码如下:

FileInputStream fileInputStream=new FileInputStream(file);
FileOutputStream outFileOutputStream=new FileOutputStream(outFile);
FileDescriptor outFileDescriptor=outFileOutputStream.getFD();
int bufferSize=1024;
byte[] buffer=new byte[bufferSize];
int readSize=-1;
while((readSize=fileInputStream.read(buffer, 0, bufferSize)) != -1) {
	outFileOutputStream.write(buffer, 0, readSize);
}
outFileOutputStream.flush();
outFileDescriptor.sync();//确保写入完成
fileInputStream.close();
outFileOutputStream.close();

8. 如何让RadioGroup中的RadioButton不可选中

在RadioGroup中的RadioButton不可选中,需要针对其中的每个RadioButton进行设置,具体过程如下:

radioGroup=....//获取RadioGroup对象
int count=radioGroup.getChildCount();//获取RadioGroup中的RadioButton数量
for(int i=0;i<count;i++){
	View view=radioGroup.getChildAt(i);//获取指定位置的控件
	view.setEnabled(false);//设置不可选中,true表示可以选中
}

也可以写成:

radioGroup=....//获取RadioGroup对象
for(int i=0;i<radioGroup.getChildCount();i++){
	radioGroup.getChildAt(i).setEnabled(false);//设置指定位置控件不可选中
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值