Android编程中的常见小问题


问题一:-dialog的半透明背景的灰度

在我们继承系统dialog实现自己的dialog时,可以通过设置style来实现
      <style name="dialog" parent="@android:style/Theme.Dialog"> 
        <item name="android:windowBackground">@android:color/transparent</item>  
        <item name="android:background">@android:color/transparent</item>  
        <item name="android:backgroundDimAmount">0.8</item>  
      </style> 
   <style name="dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:background">@android:color/transparent</item>
    <item name="android:backgroundDimAmount">0.8</item>
   </style>
android:backgroundDimAmount就是用来控制灰度的值,当为1时,界面除了我们的dialog内容是高亮显示的,dialog以外的区域是黑色的,完全看不到其他内容,系统的默认值是0.5,而已根据自己的需要调整

问题二:让文字颜色随焦点变化

button的背景是可以使用selector的,实际上文字也是支持这一特性的,首先需要定义我们的颜色:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="white">#ffffffff</color> 
    <color name="non">#ff000000</color> 
    <color name="grey">#ff505050</color> 
</resources> 
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="white">#ffffffff</color>
    <color name="non">#ff000000</color>
    <color name="grey">#ff505050</color>
</resources>

其次写我们需要的selector

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:state_pressed="true" android:color="@color/white"/> 
    <item android:state_focused="true" android:color="@color/white"/> 
    <item android:color="@color/non"/>  www.2cto.com
</selector> 
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:color="@color/white"/>
    <item android:state_focused="true" android:color="@color/white"/>
    <item android:color="@color/non"/>
</selector>

最后在需要的地方使用:android:textColor="@drawable/text"

完成。

问题三:listview滚动条相关

自定义滚动条样子,在ListView中添加属性:
android:scrollbarTrackVertical="@drawable/scrollbar_vertical_track"

android:scrollbarThumbVertical="@drawable/scrollbar_vertical_thumb"

scrollbar_vertical_trackcrollbar_vertical_thumb

自定义的xml文件,放在Drawable中,track是指长条,thumb是指短条,然后再xml中定义短条和长条的样式

不让滚动条消失,一直出现:

android:fadeScrollbars="false"

不让滚动条出现:

android:scrollbars="none"


问题四:dialog标题居中

用系统的AlertDialog.Builder创建dialog时,如果需要将dialogtitle居中显示,需要调用.setCustomTitle(View view)方法,对需要设置的view设置居中的相关属性即可实现该效果,dialog的整体展示效果需要根据自己的要求调整,当然也可以用new Dialog(Context context,int theme)的方式实现,theme需要自己定义。如下:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style parent="@android:style/Theme.Dialog" name="PauseDialog"> 
        <item name="android:windowTitleStyle">@style/PauseDialogTitle</item> 
    </style> 
    <style parent="@android:style/TextAppearance.DialogWindowTitle" name="PauseDialogTitle"> 
        <item name="android:gravity">center_horizontal</item> 
    </style> 
</resources> 

问题五:android设置gps自动开启

 1.第一种方法

private void toggleGPS() {
            Intent gpsIntent = new Intent();
            gpsIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
            gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
            gpsIntent.setData(Uri.parse("custom:3"));
            try {
                    PendingIntent.getBroadcast(StartActivity.this, 0, gpsIntent, 0).send();
            } catch (CanceledException e) {
                    e.printStackTrace();
            }
    }
 

2.第二种方法
   private void openGPSSettings() {       
      //获取GPS现在的状态(打开或是关闭状态)
    boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled( getContentResolver(), LocationManager.GPS_PROVIDER );
 
    if(gpsEnabled)
    {

    //关闭GPS
     Settings.Secure.setLocationProviderEnabled( getContentResolver(), LocationManager.GPS_PROVIDER, false );
    }
    else
    {
     //打开GPS  
     Settings.Secure.setLocationProviderEnabled( getContentResolver(), LocationManager.GPS_PROVIDER, true);

    }
   
 3.第三种方法(手动设置)

     LocationManager alm = (LocationManager)StartActivity.this.getSystemService(Context.LOCATION_SERVICE);       
       if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))
       {           
        Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();
       }       
      
       Toast.makeText(this, "请开启GPS", Toast.LENGTH_SHORT).show();
       Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
       startActivityForResult(intent,0); //此为设置完成后返回到获取界面  
   
 第一第二种需要加上权限

  <!--允许程序读取或写入系统设置 -->
 <uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>
 <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>

问题六:Android ListView元素间隙线自定义渐变效果

 首先创建一个简单的ListView,并设置对应的属性
  <ListView
       android:id="@+id/artistsNameView"
       android:layout_width="fill_parent"
      android:layout_height="match_parent"
      android:divider="@drawable/jblineshape"
      android:dividerHeight="1sp">
  </ListView>
  注意其中两句:对应的就是设置ListView中的Item之间的间隙线的,使用的一个XML文件:divershap.xml(实现的是渐变线的效果)
   
  --->> android:divider="@drawable/jblineshape"
  --->> android:dividerHeight="1sp"
 配置文件:divershap.xml,为渐变效果的,透明->白色->透明(可根据自己需要进行设置)
  
 <?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient
         android:startColor="#00000000"
         android:centerColor="#FFFFFF"
         android:endColor="#00000000"
         />
      <corners
         android:radius="4dp"
         />
  </shape>
 

问题七:Android 组件通过Intent的调用。

android通过程序打开mp3播放器播放音乐

Intent it = new Intent(Intent.ACTION_VIEW); 
 
Uri uri = Uri.parse(www.2cto.com); 
 
it.setDataAndType(uri, "audio/mp3"); 
 
startActivity(it); 
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");    
 
Intent it = new Intent(Intent.ACTION_VIEW, uri);    
 
startActivity(it);    
 

Uninstall 程序 :

Uri uri = Uri.fromParts("package", strPackageName, null);    
 
Intent it = new Intent(Intent.ACTION_DELETE, uri);    
 
startActivity(it); 
 

发送彩信

Uri uri = Uri.parse("content://media/external/images/media/23");    
Intent it = new Intent(Intent.ACTION_SEND);    
it.putExtra("sms_body", "some text");    
it.putExtra(Intent.EXTRA_STREAM, uri);    
it.setType("image/png");     
startActivity(it);   

发送SMS/MMS
调用发送短信的程序

Intent it = new Intent(Intent.ACTION_VIEW);   
it.putExtra("sms_body", "The SMS text");    
it.setType("vnd.android-dir/mms-sms");    
startActivity(it);    
 

发送短信

Uri uri = Uri.parse("smsto:0800000123");    
Intent it = new Intent(Intent.ACTION_SENDTO, uri);    
it.putExtra("sms_body", "The SMS text");    
startActivity(it);   
 

 

拨打电话:
调用拨号程序

Uri uri = Uri.parse("tel:xxxxxx"); 
Intent it = new Intent(Intent.ACTION_DIAL, uri);     
startActivity(it);    

Uri uri = Uri.parse("tel.xxxxxx"); 
Intent it =new Intent(Intent.ACTION_CALL,uri); 


要使用这个必须在配置文件中加入<uses-permission id="android.permission.CALL_PHONE" />
 

 

显示网页:

 Uri uri = Uri.parse("www.2cto.com"); 
Intent it = new Intent(Intent.ACTION_VIEW,uri); 
startActivity(it); 

显示地图:

Uri uri = Uri.parse("geo:38.899533,-77.036476"); 
Intent it = new Intent(Intent.Action_VIEW,uri); 
startActivity(it);   


路径规划:

Uri uri = Uri.parse("www.2cto.com"); 
Intent it = new Intent(Intent.ACTION_VIEW,URI); 
startActivity(it);

问题八:跳转输入法界面以及弹出输入法选择框

跳转输入法界面:
Intent intent = new Intent(); 
                intent.setAction("android.settings.INPUT_METHOD_SETTINGS"); 
                SoftDemoActivity.this.startActivity(intent); 

弹出输入法选择框
((InputMethodManager)SoftDemoActivity.this.getSystemService("input_method")).showInputMethodPicker(); 

问题九:Android隐藏软件盘

首先获得InputMethodManager的一个对象

InputMethodManager imm = (InputMethodManager)SelectWordActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);

 

调用方法

imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
其实这里的View就是EditText的对象

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值