android中小知識點積累

來源:互聯網
上載者:User

android中如何設定焦點在某一個指定的View上:
view.requestFocus();

大的捲軸設定:
listView.setFastScrollEnabled(true);

EditText游標在右邊,輸入內容從右往左:
android:gravity="center_vertical|right"
或者
editText.set
Gravity(Gravity.RIGHT);

EditText setText()之後游標會自動跑到第一個字元之前,手動設定游標的方法:
editText.setSelection(position);//position為int,指的是游標的位置,設定成EditText輸入框中字元的長度,游標則為最後了
獲得EditText中當前游標的位置的方法:
editText.getSelectionStart();

擷取狀態列高度
decorView是window中的最頂層view,可以從window中擷取到decorView,然後decorView有個getWindowVisibleDisplayFrame方法可以擷取到程式顯示的地區,包括標題列,但不包括狀態列。
於是,我們就可以算出狀態列的高度了。
   Rectframe = new Rect();    
  getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
   intstatusBarHeight = frame.top;
擷取標題列高度:
getWindow().findViewById(windows.iD_ANDROID_CONTENT)這個方法擷取到的view就是程式不包括標題列的部分,然後就可以知道標題列的高度了。
  intcontentTop =getWindow().findViewById(windows.iD_ANDROID_CONTENT).getTop();  
 //statusBarHeight是上面所求的狀態列的高度  
  inttitleBarHeight = contentTop - statusBarHeight;

//隱藏軟鍵盤 
 ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(YourActivity.this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
//顯示軟鍵盤,控制項ID可以是EditText,TextView    ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).showSoftInput(控制項ID,0);

 

倆按鈕的AlertDialog.Bulder:

newAlertDialog.Builder(this).setMessage("是否刪除該項") 
       .setPositiveButton("確定", new DialogInterface.OnClickListener(){  
           public void onClick(DialogInterface dialog, int which) {
           deleteInfo(id);
          }
    })
    .setNegativeButton("取消", new DialogInterface.OnClickListener(){
             publicvoid onClick(DialogInterface dialog, int which) {
    }
    }).show();

 

設定EditText是否有游標:

android:cursorVisible="false" //為false時選中了也沒有游標

或editText.setCursorVisible(visible);

 

View
···
getLocalVisibleRect , 返回一個填充的Rect對象,感覺是這個View的Rect大小,left,top取到的都是0
···
getGlobalVisibleRect , 擷取全域座標系的一個視圖地區,返回一個填充的Rect對象;該Rect是基於總整個螢幕的
···
getLocationOnScreen,計算該視圖在全域座標系中的x,y值,(注意這個值是要從螢幕頂端算起,也就是索包括了通知欄的高度)//擷取在當前螢幕內的絕對座標
···
getLocationInWindow ,計算該視圖在它所在的widnow的座標x,y值

···
getLeft , getTop,getBottom,getRight,  這一組是擷取相對在它父親裡的座標

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.