在fragment中监听返回键,home键
2016-04-27 15:24
351 查看
android 在fragment中如何监听返回键,home键
摘自:http://www.myexception.cn/android/1739468.html
在activity中用keydown很容易实现对返回键的监听,但是这个函数不能再fragment中重载。
通过我呕心沥血的寻找,终于找到了解决办法,对其他物理按键的监听也同理。
摘自:http://www.myexception.cn/android/1739468.html
在activity中用keydown很容易实现对返回键的监听,但是这个函数不能再fragment中重载。
通过我呕心沥血的寻找,终于找到了解决办法,对其他物理按键的监听也同理。
public class phonerecorder extends Fragment { View listview; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { listview = (View) inflater.inflate(R.layout.phonerecorder, null); mListView = (ListView) listview.findViewById(R.id.listView); init(); mListView.setOnItemClickListener(clickitemlistener); listview.setFocusable(true);//这个和下面的这个命令必须要设置了,才能监听back事件。 listview.setFocusableInTouchMode(true); listview.setOnKeyListener(backlistener); return listview; } private View.OnKeyListener backlistener = new View.OnKeyListener() { @Override public boolean onKey(View view, int i, KeyEvent keyEvent) { if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) { if (i == KeyEvent.KEYCODE_BACK) { //表示按返回键 时的操作 if (!rootpatch.equals(currentfilepach) && currentfilepach != null) { File file = new File(currentfilepach); openDir2(file.getParent().toString()); currentfilepach = file.getParent().toString(); return true; } //后退 return false; //已处理 } } return false; } }; }
相关文章推荐
- J2SE 之 基本语法
- Lua学习(四)函数
- zk007 zookeeper curator api
- 5.0SystemUI布局
- 使用虚拟机安装kali Linux遇到的运行问题
- DateTime 的使用技巧
- C#正则表达式 Regex 常用API
- Android中的dp,px,sp互转问题以及 View.setLayoutParams, 以及网络相关工具类
- 解决easyui 控件datagrid 二次加载问题
- 文件的输入输出
- 内存溢出
- C# 字符串中判断存在的几种模式和效率(string.contains、string.IndexOf、Regex.Match)
- ViewPager 详解(四)----自主实现滑动指示条
- IOS 写一个整洁易用的tableView
- C# chart
- 文章标题
- NetworkInterface获取主机ip,判断内外网
- 啊哈!算法—小哼买书
- utf8汉字编码16进制对照
- 工具类整理 php 文件处理 二维码生成 laravel 移动设备判断