代码片段--Button抢焦点, 无论点在哪都是button触发. 解决方法很简单, 在item最外层的布局中加一个配置:

收集一些没有必要或者无法写到工具类中的代码片段

亮度调节/屏幕亮度调节

package android.lekko.tools;

import android.app.Activity;
import android.content.ContentResolver;
import android.provider.Settings;
import android.provider.Settings.System;
import android.view.WindowManager;
import android.widget.Toast;

public class LightnessControl {
    // 判断是否开启了自动亮度调节 
    public static boolean isAutoBrightness(Activity act) { 
        boolean automicBrightness = false; 
        ContentResolver aContentResolver = act.getContentResolver();
        try { 
            automicBrightness = Settings.System.getInt(aContentResolver, 
                   Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; 
        } catch (Exception e) { 
            Toast.makeText(act,"无法获取亮度",Toast.LENGTH_SHORT).show();
        } 
        return automicBrightness; 
    }     
    // 改变亮度
    public static void SetLightness(Activity act,int value)
    {        
        try {
            System.putInt(act.getContentResolver(),System.SCREEN_BRIGHTNESS,value); 
            WindowManager.LayoutParams lp = act.getWindow().getAttributes(); 
            lp.screenBrightness = (value<=0?1:value) / 255f;
            act.getWindow().setAttributes(lp);
        } catch (Exception e) {
            Toast.makeText(act,"无法改变亮度",Toast.LENGTH_SHORT).show();
        }        
    }
    // 获取亮度
    public static int GetLightness(Activity act)
    {
        return System.getInt(act.getContentResolver(),System.SCREEN_BRIGHTNESS,-1);
    }
    // 停止自动亮度调节 
    public static void stopAutoBrightness(Activity activity) { 
        Settings.System.putInt(activity.getContentResolver(), 
                Settings.System.SCREEN_BRIGHTNESS_MODE, 
                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); 
    }
    // 开启亮度自动调节 
    public static void startAutoBrightness(Activity activity) { 
        Settings.System.putInt(activity.getContentResolver(), 
                Settings.System.SCREEN_BRIGHTNESS_MODE, 
                Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); 
    } 
}

调节音量

步骤1:或许系统音量对象的实例
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
步骤2:获取各种音量情报
Android的音量信息一共有7种。
①通话音量
int max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_VOICE_CALL );
int current = mAudioManager.getStreamVolume( AudioManager.STREAM_VOICE_CALL );
Log.d(”VIOCE_CALL”, “max : ” + max + ” current : ” + current);
②系统音量
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_SYSTEM );
current = mAudioManager.getStreamVolume( AudioManager.STREAM_SYSTEM );
Log.d(”SYSTEM”, “max : ” + max + ” current : ” + current);
③铃声音量
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_RING );
current = mAudioManager.getStreamVolume( AudioManager.STREAM_RING );
Log.d(”RING”, “max : ” + max + ” current : ” + current);
④音乐音量
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_MUSIC );
current = mAudioManager.getStreamVolume( AudioManager.STREAM_MUSIC );
Log.d(”MUSIC”, “max : ” + max + ” current : ” + current);
⑤提示声音音量
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_NOTIFICATION );
current = mAudioManager.getStreamVolume( AudioManager.STREAM_NOTIFICATION );
Log.d(”NOTIFICATION”, “max : ” + max + ” current : ” + current); 
⑥闹铃音量 
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_ALARM );
current = mAudioManager.getStreamVolume( AudioManager.STREAM_ALARM );
Log.d(”ALARM”, “max : ” + max + ” current : ” + current);
⑦DTMF(Double Tone MulitiFrequency,双音多频)
设置音量的方法也很简单,AudioManager提供了方法:
public void setStreamVolume(int streamType, int index, int flags)
其他:
各种音量有不同的播放方式,例如,音乐就应该用MediaPlayer来播放。
private MediaPlayer mediaPlayer=null; //声频
mediaPlayer=MediaPlayer.create(AudioActivity.this, R.raw.music);
mediaPlayer.setLooping(true);//设置循环播放
mediaPlayer.start();//播放声音
而铃声就应该用RingtoneManager来播放。
如果混用的话,参照的音量就会不正确。
这个时候可以通过设定流类型来改善。
例如
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mRingtone = RingtoneManager.getRingtone(getApplicationContext(), uri);
RingtoneManager是播放铃声的,如果用来播放通知音,
就需要把声音的流类型改成AudioManager.STREAM_NOTIFICATION
mRingtone.setStreamType(AudioManager.STREAM_NOTIFICATION );

ListView 中有 Button

Button抢焦点, 无论点在哪都是button触发. 解决方法很简单, 在item最外层的布局中加一个配置:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability= "blocksDescendants" >
    <TextView
        android:id="@+id/item_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="TextView" />
    <Button
        android:id="@+id/item_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="Button" />
</RelativeLayout>  
如果有必要的话, 给抢焦点的控件加一个 : android:focusable = "false"
关于android:descendantFocusability,有三个属性值,简述如下表,详述可参考这篇博文说明。
beforeDescendants: viewgroup会优先其子类控件而获取到焦点
afterDescendants: viewgroup只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants: viewgroup会覆盖子类控件而直接获得焦点

在代码中修改 TextView 上下左右的图片

Drawable top=getResources().getDrawable(R.drawable.netmanager);
// 注意这行代码必须有
top.setBounds(0, 0, top.getMinimumWidth(), top.getMinimumHeight());
tv.setCompoundDrawables(null, top, null, null);

自定义 Dialog 和软键盘共存在屏幕上

自定义的dialog要想和软键盘共同存在屏幕上需要给window设置这个属性
 
alertDialog不需要设置是因为它本身就创建在有这个属性的window上面
 
 

去掉一些控件的默认背景

EditText不要黄色的边框(2.3), 也不要下面的蓝线(4.0)
灰常简单: android:background="@null" 或者 android:background="@android:drawable/transparent"
ListView不要点击时的默认背景
android:listSelector="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"





posted @ 2016-10-01 00:56  杨伟乔  阅读(730)  评论(0编辑  收藏  举报