当前位置:首页 » 安卓系统 » androidisvisible

androidisvisible

发布时间: 2024-12-28 17:51:43

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; 琛ㄧず鍑虹幇阌栾甯搁噺銆

热点内容
如何触发安卓小游戏 发布:2024-12-29 09:00:24 浏览:955
ftpcentos虚拟用户 发布:2024-12-29 08:59:41 浏览:947
数星星脚本 发布:2024-12-29 08:59:05 浏览:656
编译器可以单独用吗 发布:2024-12-29 08:58:09 浏览:853
天籁汽车有哪些配置 发布:2024-12-29 08:56:07 浏览:55
哈曼印象哪个配置好 发布:2024-12-29 08:48:16 浏览:530
华为存储少 发布:2024-12-29 08:08:43 浏览:287
php获取文件创建时间 发布:2024-12-29 08:06:27 浏览:921
为什么淘宝缓存是0 发布:2024-12-29 08:01:00 浏览:705
pythonstring转json 发布:2024-12-29 07:55:15 浏览:491