androidisvisible
A. android 在fragment中怎麼監聽返回鍵,home鍵
android 在fragment中監聽返回鍵和home鍵的方法:
思路:
1、定義OnBackPressedListener監聽介面
2、創建基礎監聽類,實現OnBackPressedListener介面
3、在BaseActivity中創建方法調用
4、重寫onBackPressed方法和onHomePress方法
核心代碼如下:
@Override
public void onBackPressed() {
FragmentManager fragmentManager =getSupportFragmentManager();
if (((C_fragment) getSupportFragmentManager().findFragmentByTag("xyz")) != null && ((C_fragment) getSupportFragmentManager().findFragmentByTag("xyz")).isVisible()) {
Fragment fragment = new D_Fragment();
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
getSupportActionBar().setTitle("D fragment ");
} else {
super.onBackPressed();
}
}
@Override
public void onHomePressed() {
if (onBackPressedListener != null)
onBackPressedListener.doBack();
else
super.onBackPressed();
B. Android騫沖彴楂樺痙API緇忛獙錛氶暱鎸夊湴鍥捐幏鍙栦綅緗淇℃伅
Android寮鍙戣繃紼嬩腑,鏈夊緢澶氭墜鍔挎搷浣滈兘寰堣╀漢鍙堢埍鍙堟仺銆備竴鏂歸潰鍙浠ユ洿渚挎嵎鐨勪綋鐜版洿澶氬姛鑳,鎻愬崌搴旂敤鐨勪綋楠,涓鏂歸潰綣佸嶅氬彉鐨勬搷浣滆儗鍚庢湁鐫璁稿氬紑鍙戞妧宸у拰闅鵑樸傝繖閲屽垎浜涓涓闀挎寜鍦板浘鑾峰彇浣嶇疆淇℃伅鐨勬墜鍔褲 涓轟簡鏇存槑浜,鍏堜笂涓灞曠ず鏁堟灉:闀挎寜鍦板浘鏌愮偣鏄劇ず璇ョ偣鍦扮悊浣嶇疆淇℃伅鍔熻兘 閫氳繃鏋勯犱竴涓猯ocationSelectOverlay綾繪潵瀹氫箟璇ュ姛鑳,鍦ㄥ湴鍥句笂瀵歸暱鎸夋墜鍔胯繘琛岀洃鍚,涓鏃︽湁榪欎釜浜嬩歡鍙戠敓灝辮皟鐢╣etAddressFromServer()鏂規硶鏉ユ樉紺哄湴鍧淇℃伅銆 鍦ㄨュ伐紼嬩腑鍒嗗埆瀹氫箟4涓綾籰ongPressMap.java,locationSelectOverlay.java,popUpPanel.java,Constants.java longPressMap.java涓烘樉紺轟竴涓鍦板浘綾,閫氳繃瀹炰緥鍖栦竴涓猯ocationSelectOverlay綾誨疄鐜伴暱鎸夊湴鍥炬樉紺哄湴鐞嗕綅緗淇℃伅鍔熻兘浠g爜濡備笅: //longPressMap 綾葷戶鎵縈apActivity瀵筸apview璧勬簮榪涜岀$悊 public class longPressMap extends MapActivity { private MapView mMapView; locationSelectOverlay mSelectLay; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //浣跨敤setContentView鏂規硶璋冪敤R.layout.activity_regeocoder甯冨矓鏂囦歡,鏄劇ず鍦板浘 setContentView(R.layout.geocoder); //鑾峰彇鍦板浘瑙嗗浘鐨剗d,璧嬪肩粰mMapView mMapView = ((MapView) findViewById(R.id.geocode_MapView)); // 璁劇疆鍚鐢ㄥ唴緗鐨勭緝鏀炬帶浠 mMapView.setBuiltInZoomControls(true); //瀹炰緥鍖栦竴涓猯ocationSelectOverlay綾 mSelectLay = new locationSelectOverlay(this, mMapView, new popUpPanel(this, mMapView)); //灝嗚ュ姛鑳藉姞杞藉埌姝ゅ湴鍥句笂,鍚鐢ㄩ暱鎸夊湴鍥炬樉紺鴻ョ偣鍦板潃淇℃伅鐨勫姛鑳 mMapView.getOverlays().add(mSelectLay); } } 澶嶅埗浠g爜 locationSelectOverlay紺轟緥浠g爜濡備笅: //locationSelectOverlay綾葷戶鎵縊verlay鎺ュ彛,瀹炵幇OnGestureListener鎵嬪娍鐩戝惉 public class locationSelectOverlay extends Overlay implements OnGestureListener { public popUpPanel mTipPanel; //澹版槑涓涓寮瑰嚭妗嗗硅薄 GeoPoint mSelectPoint; //澹版槑涓涓鍦扮悊鍧愭爣鐐瑰硅薄 MapView mMapView; //澹版槑涓涓鍦板浘瑙嗗浘瀵硅薄 Context mContext; //媧誨姩瀵硅薄 TextView mTipText=null; //澹版槑涓涓鏂囨湰瀵硅薄 private static String nearbystr=""; private GestureDetector gestureScanner; //澹版槑涓涓鎵嬪娍鐩戝惉瀵硅薄 privateGeocoder coder; //澹版槑涓涓閫嗗湴鐞嗙紪鐮佸硅薄 private String addressName=""; //澹版槑涓涓鍦板潃鍚嶇О瀛楃︿覆 //闀挎寜鍦板浘鏌愮偣鑾峰彇淇℃伅鐨勬瀯閫犲嚱鏁般 public locationSelectOverlay(Activity context,MapView mapView,popUpPanel panel) { this.mContext=context; this.mMapView=mapView; this.mTipPanel=panel; gestureScanner = new GestureDetector(this); //澹版槑涓涓鎵嬪娍鐩戝惉瀵硅薄 coder = new Geocoder(context); //澹版槑涓涓閫嗗湴鐞嗙紪鐮佸硅薄 } //鐢℉andler鍑芥暟澶勭悊浼犻掓潵鐨勫湴鍧淇℃伅,鏄劇ず鍦ㄦ枃鏈妗嗕腑 private Handler mGeocoderHandler = new Handler() { public void handleMessage(Message msg) { //濡傛灉鏈夊湴鍧淇℃伅鐨勬秷鎮鍙戦佽繃鏉,灝嗘枃鏈妗嗕腑璁劇疆涓鴻ュ湴鍧淇℃伅 if(msg.what == Constants.REOCODER_RESULT) { if(mTipText!=null) mTipText.setText(addressName); } //濡傛灉鏄劇ず閿欒,鍒欐枃鏈妗嗕腑璁劇疆鎶ラ敊淇℃伅 else if(msg.what == Constants.ERROR) { Toast.makeText(mContext, "鑾峰彇鍦板潃澶辮觸,璇烽噸璇", Toast.LENGTH_SHORT).show(); removeTipPanel(); } } }; //鏄劇ず寮瑰嚭紿楀彛 public boolean showTap(GeoPoint p) { View view = mTipPanel.getView(); mMapView.removeView(view); //甯冨矓鍙傛暟璁劇疆 MapView.LayoutParams geoLP = new MapView.LayoutParams( MapView.LayoutParams.WRAP_CONTENT, MapView.LayoutParams.WRAP_CONTENT, p, MapView.LayoutParams.BOTTOM_CENTER); //寮瑰嚭紿楀彛鐨勬枃鏈鏄劇ず mTipText = (TextView) view.findViewById(R.id.GeoName); mTipText.setText("姝e湪鍔犺澆鍦板潃..."); mTipText.setOnClickListener(new OnClickListener() { public void onClick(View v) { } }); //鍦ㄥ湴鍥捐嗗浘涓婃坊鍔犺ュ脊鍑虹獥鍙h嗗浘 mMapView.addView(view, geoLP); return false; } //浠庣粡綰搴﹀潗鏍囩偣鑾峰彇瀵瑰簲鐨勫湴鍧淇℃伅 publicvoid getAddressFromServer(final GeoPoint point,final Handler handler) { //澹版槑涓涓綰跨▼ new Thread(){ public void run() { try { // 閫嗗湴鐞嗙紪鐮乬etFromLocation()鍑芥暟鑾峰彇璇ョ偣瀵瑰簲鐨勫墠3涓鍦板潃淇℃伅 ListAddress address = coder.getFromLocation((double)point.getLatitudeE6()/1E6, (double)point.getLongitudeE6()/1E6, 3); if (address != null) { //鑾峰彇絎涓涓鍦板潃淇℃伅 Address addres = address.get(0); addressName = ""; if(addres.getAdminArea()!=null) addressName+=addres.getAdminArea(); if(addres.getSubLocality()!=null) addressName += addres.getSubLocality(); if(addres.getFeatureName()!=null) addressName += addres.getFeatureName(); addressName += "闄勮繎"; handler.sendMessage(Message .obtain(handler, Constants.REOCODER_RESULT)); } } catch (AMapException e) { // TODO Auto-generated catch block handler.sendMessage(Message .obtain(handler, Constants.ERROR)); } } }.start(); //綰跨▼鍚鍔 } //縐昏蛋寮瑰嚭紿楀彛 public void removeTipPanel() { View view = mTipPanel.getView(); mMapView.removeView(view); } //鑾峰彇鎵嬪娍鎿嶄綔 public boolean onTouchEvent(MotionEvent event, MapView mapView) { return gestureScanner.onTouchEvent(event); } @Override public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // TODO Auto-generated method stub return false; } //闀挎寜鍦板浘,寮瑰嚭鎻愮ず妗,鏄劇ず璇ョ偣鍦板潃淇℃伅 @Override public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub int x = (int)e.getX(); int y = (int)e.getY(); mSelectPoint = mMapView.getProjection().fromPixels(x, y); //璋冪敤鏄劇ず鎻愮ず妗嗗嚱鏁 showTap(mSelectPoint); //璋冪敤浠庣粡綰搴︾偣鑾峰彇鍦板潃淇℃伅鍑芥暟 getAddressFromServer(mSelectPoint,mGeocoderHandler); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub return false; } 澶嶅埗浠g爜 pouUpPanel瀹氫箟浜嗗脊鍑虹獥鍙g被 public class popUpPanel { private boolean isVisible = false; private MapView mMapView; private View popup; public popUpPanel(Activity paramActivity, MapView paramMapView) { this.mMapView = paramMapView; ViewGroup localViewGroup = (ViewGroup)this.mMapView.getParent(); //璁劇疆寮瑰嚭鐨勮嗗浘鏄痠d涓篟.layout.activity_long_press_map鐨勮嗗浘 this.popup = paramActivity.getLayoutInflater().inflate(R.layout.activity_long_press_map, localViewGroup, false); 鈥 澶嶅埗浠g爜 Constants 瀹氫箟浜嗕紶閫掔殑甯擱噺瀵瑰簲鐨勫礆紝濡俻ublic static finalint REOCODER_RESULT=3000; 琛ㄧず閫嗗湴鐞嗙紪鐮佺粨鏋滃父閲忥紝public staticfinal int ERROR=1001; 琛ㄧず鍑虹幇閿欒甯擱噺銆