android技术片段

一。不让程序默认升起IME输入框有两种方法:
1.让EditText失去焦点,使用EditText的clearFocus方法
2.强制隐藏Android输入法窗口,在IME类中我们通过实例化输入法控制对象,通过hideSoftInputFromWindow来隐藏IME输入框。

Toast.makeText(WindowBackgroundColorActivity.this, "焦点改变", Toast.LENGTH_SHORT).show();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
//第一种方法
//imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
//第二种方法
imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

 

 

二。android中要获取屏幕的分辨率,需要用到DisplayMetrics这个类,具体如下:

//获取屏幕大小
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
String str = "屏幕分辨率(单位:像素)为:"
       + dm.widthPixels + " x " + dm.heightPixels;

 

 

 

   //全屏显示
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        //      WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
 
 
 四。popupwindow
showAtLocation(findViewById(R.id.edit_layout), Gravity.BOTTOM,0, 0);  
设置弹窗的位置:
第一个参数是弹窗父控件的布局;
第二个参数是位置如左,右,上部,下部等;
第三个参数是X方向的偏移量;
第四个参数是Y方向的偏移量。
showAtLocation(parent, Gravity.RIGHT | Gravity.BOTTOM, 10,10);
第一个参数指定PopupWindow的锚点view,即依附在哪个view上。
第二个参数指定起始点为parent的右下角,第三个参数设置以parent的右下角为原点,向左、上各偏移10像素。

//将PopupWindow作为anchor的下拉窗口显示。即在anchor的左下角显示
showAsDropDown(anchor);
//xoff,yoff基于anchor的左下角进行偏移。
showAsDropDown(anchor, xoff, yoff);

 

五。TextView要让文本垂直/水平居中显示,有两种情况需要考虑:

1、layout_width/layout_height为wrap_content,此时要让TextView在父控件上居中显示,必须设置layout_gravity=”center”。
2、layout_width/layout_height为fill_parent,此时由于TextView已占据父窗体所有空间,必须设置gravity=”center”。

android:gravity用来设置TextView的内容对齐方式,android:layout_gravity用来设置TextView在父窗体中的对齐方式。

 

六半透明背景

android:background="#b0000000" #e0000000  “#b0000000”

七。让程序的界面不随机器的重力感应而翻转

<activity android:screenOrientation="portrait"> </activity> 

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

RelativeLayout 里面加上android:clickable="true" RelativLayout就会出现在selector里面定义的效果。

在文字中加下划线:  textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

 

八.虚拟键盘弹出时 把页面内容上移

<activity name="EditContactActivity"
android:windowSoftInputMode="stateVisible|adjustResize">
...
</activity>

九:点击空白处隐藏软键盘

 

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

    @Override   
        public   boolean  onTouchEvent(MotionEvent event) {  
            // TODO Auto-generated method stub   
            if  (event.getAction() == MotionEvent.ACTION_DOWN) {  
                System.out.println("down" );  
                if  (RegisterActivity. this .getCurrentFocus() !=  null ) {  
                    if  (RegisterActivity. this .getCurrentFocus().getWindowToken() !=  null ) {  
                        imm.hideSoftInputFromWindow(RegisterActivity.this .getCurrentFocus().getWindowToken(),  
                                InputMethodManager.HIDE_NOT_ALWAYS);  
                    }  
                }  
            }  
            return   super .onTouchEvent(event);  
        } 

 

十,scrollview+listview的高度自动适应自适应

很多时间我们在scorllview中嵌入listview的时候,都只能看到listview显示一行数据,而我们的要求是显示多行,即我们数据的行数,那么请看下面的代码:

                int totalHeight = 0;//总的高度
for (int i = 0; i < initData(0).size(); i++) { //initData(0).size()数据的行数
View listItem = myAdapter.getView(i, null, list1); //list1,当前listview
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}

ViewGroup.LayoutParams params = list1.getLayoutParams();
params.height = totalHeight
+ (list1.getDividerHeight() * (myAdapter.getCount() - 1));
list1.setLayoutParams(params);

 

 

 

退出应用程序的实现:可以自己写个方法,例如:

3
4
5
6
7
8
9
    public void exitProgrames(){
    Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startMain);
            android.os.Process.killProcess(android.os.Process.myPid());
}

需要添加权限:<uses-permission android:name="android.permission.RESTART_PACKAGES" />

 

 



 
 
 

原文地址:https://www.cnblogs.com/jiezzy/p/2490399.html