Android总结【不定期更新】

全屏显示:

this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

继承Activity下取消标题栏:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

继承AppCompatActivity下取消标题栏:

this.getSupportActionBar().hide();

隐藏导航栏

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View decorView = getWindow().getDecorView();
int option = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        | View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(option);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();

透明状态栏(内容填充至状态栏下部)

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//平台版本需大于等于android 5.0
if (Build.VERSION.SDK_INT >= 21) {
    View decorView = getWindow().getDecorView();
    
    int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
    decorView.setSystemUiVisibility(option);
    //设置状态栏透明
    getWindow().setStatusBarColor(Color.TRANSPARENT);
}

ActionBar actionBar = getSupportActionBar();
//隐藏标题栏
actionBar.hide();

该条引自郭神大佬!!!:https://blog.csdn.net/guolin_blog/article/details/51763825


感谢大佬:https://www.cnblogs.com/xiangevan/p/7818159.html

Android动态设置TextView的颜色的四种方法

package com.txlong;    
    
import android.app.Activity;    
import android.graphics.Color;    
import android.os.Bundle;    
import android.widget.TextView;    
    
public class ListViewDemoActivity extends Activity {    
    // private ListView listView;    
    
    /** Called when the activity is first created. */    
    @Override    
    public void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);    
    
        TextView tv = new TextView(this);    
        tv.setText("Test set TextView's color.");    
        //方案一:通过ARGB值的方式    
        /**  
         * set the TextView color as the 0~255's ARGB,These component values  
         * should be [0..255], but there is no range check performed, so if they  
         * are out of range, the returned color is undefined  
         */    
//      tv.setTextColor(Color.rgb(255, 255, 255));    
        /**  
         * set the TextView color as the #RRGGBB #AARRGGBB 'red', 'blue',  
         * 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow',  
         * 'lightgray', 'darkgray'  
         */    
        tv.setTextColor(Color.parseColor("#FFFFFF"));    
            
            
        /** 原来不知道有上边的方法,就用这个笨笨方法了 */    
//      String StrColor = null;    
//      StrColor = "FFFFFFFF";    
//      int length = StrColor.length();    
//      if (length == 6) {    
//          tv.setTextColor(Color.rgb(    
//                  Integer.valueOf(StrColor.substring(0, 2), 16),    
//                  Integer.valueOf(StrColor.substring(2, 4), 16),    
//                  Integer.valueOf(StrColor.substring(4, 6), 16)));    
//      } else if (length == 8) {    
//          tv.setTextColor(Color.argb(    
//                  Integer.valueOf(StrColor.substring(0, 2), 16),    
//                  Integer.valueOf(StrColor.substring(2, 4), 16),    
//                  Integer.valueOf(StrColor.substring(4, 6), 16),    
//                  Integer.valueOf(StrColor.substring(6, 8), 16)));    
//      }    
            
        //方案二:通过资源引用    
//      tv.setTextColor(getResources().getColor(R.color.my_color));    
            
        //方案三:通过资源文件写在String.xml中    
//      Resources resource = (Resources) getBaseContext().getResources();    
//      ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.my_color);    
//      if (csl != null) {    
//          tv.setTextColor(csl);    
//      }    
    
        //方案四:通过xml文件,如/res/text_color.xml    
//      XmlPullParser xrp = getResources().getXml(R.color.text_color);    
//      try {    
//          ColorStateList csl = ColorStateList.createFromXml(getResources(), xrp);    
//          tv.setTextColor(csl);    
//      } catch (Exception e) {    
//      }    
            
        // listView = new ListView(this);    
        //    
        // Cursor cursor = getContentResolver().query(    
        // Uri.parse("content://contacts/people"), null, null, null, null);    
        //    
        // startManagingCursor(cursor);    
        //    
        // ListAdapter listAdapter = new SimpleCursorAdapter(this,    
        // android.R.layout.simple_expandable_list_item_2, cursor,    
        // new String[] { "name", "name" }, new int[] {    
        // android.R.id.text1, android.R.id.text2 });    
        //    
        // listView.setAdapter(listAdapter);    
        // setContentView(listView);    
        setContentView(tv);    
    }    
}    

感谢大佬:https://blog.csdn.net/liuming_nx/article/details/17583905

View.setBackgroundColor(Color.BLUE); //背景颜色设置为 Blue
View.setBackgroundResource(0)//移除背景,包括:背景颜色,图片等等

感谢大佬:https://blog.csdn.net/gf771115/article/details/8230176

Android中dip(dp)与px之间单位转换

/**
 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
 */
public static int dip2px(Context context, float dpValue) {
 final float scale = context.getResources().getDisplayMetrics().density;
 return (int) (dpValue * scale + 0.5f);
}

 

/**
 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
 */
public static int px2dip(Context context, float pxValue) {
 final float scale = context.getResources().getDisplayMetrics().density;
 return (int) (pxValue / scale + 0.5f);
}

注:后面的0.5f是因为转int类型还要四舍五入,为保证精准度。

android 判断edittext是否为空

感谢大佬:
Android中EditText就是文本输入控件,它的值是个String类型,

判断输入是否为空可以通过String TextUtil 等API来判断

有以下几种方式:https://zhidao.baidu.com/question/710806300136762005.html

直接判断EditText的长度editText.length() 如果等于0则为空

通过TextUtil.isEmpty(editText.getText()) true表示是空,false表示非空

通过正则表达式
通过String.length() 判断长度

以下为示例代码,如果为空,则跳出提示:

String txt = editText.getText().toString();

if(txt.length() == 0){

    Toast.makeText(context,"输入不能为空",0).show();    //弹出一个自动消失的提示框
    return;

}

补充:占位符 %1$s %1$d

感谢大佬:https://blog.csdn.net/jiangtea/article/details/71156047

%n$ms:代表输出的是字符串,n代表是第几个参数,设置m的值可以在输出之前放置空格

%n$md:代表输出的是整数,n代表是第几个参数,设置m的值可以在输出之前放置空格,也可以设为0m,在输出之前放置m个0

%n$mf:代表输出的是浮点数,n代表是第几个参数,设置m的值可以控制小数位数,如m=2.2时,输出格式为00.00
在string.xml中

<string name="alert">我的名字叫%1$s,我来自%2$s,我今年%3$d岁了</string>

代码

String string= getResources().getString(R.string.alert);
tv.setText(String.format(string, "隔壁老汪","上海",30));

在这里插入图片描述
补充:注意符号问题!!!

原文地址:https://www.cnblogs.com/tfxz/p/12621719.html