android-电话窗口弹出以及LayoutInflater和inflate的用法
来源:互联网 发布:php 发送xml格式报文 编辑:程序博客网 时间:2024/04/30 12:41
最近在做android项目的时候要用到一个从底部弹出电话窗口功能,差了很多资料,也有很多说法,自己在这里做简单总结,为以后方便实用。
首先在所需要调用的activity做如下声明
参数声明
//phoneNumber界面参数 private LayoutInflater inflater; private EditText phone; private Button delete; private View viewPhone;
在onCreate方法中加入
//phoneNumber界面参数定义inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);viewPhone= inflater.inflate(R.layout.talkphone, null);phone = (EditText) viewPhone.findViewById(R.id.phoneEdit);delete = (Button) viewPhone.findViewById(R.id.phoneback);
在这里我们用到了一个 LayoutInflater和inflater,这里对这两个类型作简要说明
/** * LayoutInflater这个类的作用类似于findViewById(), * 不同点: * LayoutInflater是用来找layout下xml布局文件的,而且它会实例化 * findViewById()是找具体xml布局文件下的具体widget控件,比如:Button按钮 * * * * inflate就相当于将一个xml中定义的布局找出来. * 因为如果在一个Activity文件里直接用findViewById()这个方法的话, * 那么它所对应的是setConentView()中调用的那个layout里的组件. * 因此如果在同样的Activity里用到别的layout的话, * 而且你还要设置这个layout里的组件(比如:ImageView,TextView)上的内容, * 那么你就必须用inflate()先将这个layout找出来, 然后再用这个layout对象去找到它上面的组件 * 然后进行一系列的操作 * * inflate()方法中参数: * 1.想要用的布局文件的id * 2.持有选项卡的内容,获取FrameLayout * 3.true:将此处解析的xml文件做为根视图View */具体方式如下
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.custom_dialog,null); TextView text = (TextView) layout.findViewById(R.id.text);
电话窗口弹出的方法
private void showPopwindow() {// 利用layoutInflater获得ViewView v = (Button) viewPhone.findViewById(R.id.number0);v.setOnClickListener(this);for(int i=0;i<5;i++){v = viewPhone.findViewById(R.id.number1 + i);v.setOnClickListener(this);}for(int i = 0;i<4;i++){v = viewPhone.findViewById(R.id.number6 + i);v.setOnClickListener(this);}delete.setOnClickListener(this);delete.setOnLongClickListener(new OnLongClickListener() {public boolean onLongClick(View v) {phone.setText("");return false;}});// 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()PopupWindow window = new PopupWindow(viewPhone,WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);window.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);// 设置popWindow弹出窗体可点击,这句话必须添加,并且是truewindow.setFocusable(true);// 实例化一个ColorDrawable颜色为半透明ColorDrawable dw = new ColorDrawable(0xb0000000);window.setBackgroundDrawable(dw);// 设置popWindow的显示和消失动画window.setAnimationStyle(R.style.mypopwindow_anim_style);// 在底部显示window.showAtLocation(Room.this.findViewById(R.id.call_recv),Gravity.BOTTOM, 0, -300);/*// 这里检验popWindow里的button是否可以点击Button first = (Button) view.findViewById(R.id.number1);first.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {System.out.println("第一个按钮被点击了");}});*///popWindow消失监听方法window.setOnDismissListener(new OnDismissListener() {@Overridepublic void onDismiss() {System.out.println("popWindow消失");}});}@Overridepublic void onClick(View v){switch(v.getId()){case R.id.number0:if(phone.getText().length()<12){input(v.getTag().toString());break;}case R.id.number1:if(phone.getText().length()<12){input(v.getTag().toString());break;}case R.id.number2:if(phone.getText().length()<12){input(v.getTag().toString());break;}case R.id.number3:if(phone.getText().length()<12){input(v.getTag().toString());break;}case R.id.number4:if(phone.getText().length()<12){input(v.getTag().toString());break;}case R.id.number5:if(phone.getText().length()<12){input(v.getTag().toString());break;}case R.id.number6:if(phone.getText().length()<12){input(v.getTag().toString());break;}case R.id.number7:if(phone.getText().length()<12){input(v.getTag().toString());break;}case R.id.number8:if(phone.getText().length()<12){input(v.getTag().toString());break;}case R.id.number9:if(phone.getText().length()<12){input(v.getTag().toString());break;}case R.id.phoneback:delete();break;}}private void input(String str){int c = phone.getSelectionStart();String p = phone.getText().toString();phone.setText(p.substring(0, c) + str + p.substring(phone.getSelectionStart(), p.length()));phone.setSelection(c + 1, c + 1);}private void delete() {int c = phone.getSelectionStart();if (c > 0) {String p = phone.getText().toString();phone.setText(p.substring(0, c - 1) + p.substring(phone.getSelectionStart(), p.length()));phone.setSelection(c - 1, c - 1);}}
这里的showPowwindow()方法就是从底部弹出一个对话框。
然后需要添加两个XML来设置出现和消失的动画效果,具体XML实现方式如下,我们给他起名字叫pophidden_anim
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="200" android:fromYDelta="0" android:toYDelta="50%p" /> <alpha android:duration="200" android:fromAlpha="1.0" android:toAlpha="0.0" /></set>
另一个XML的实现方式如下,我们给他起名叫popshow_anim
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="200" android:fromYDelta="100%p" android:toYDelta="0" /> <alpha android:duration="200" android:fromAlpha="0.0" android:toAlpha="1.0" /></set>
接下来再styles文件里加入显示和隐藏的HTML
<style name="mypopwindow_anim_style"> <item name="android:windowEnterAnimation">@anim/popshow_anim</item> <!-- 指定显示的动画xml --> <item name="android:windowExitAnimation">@anim/pophidden_anim</item> <!-- 指定消失的动画xml --> </style>
最后是自己写的拨号界面,你自己的layout下边的拨号XML文件
这样就完成了拨号界面的调用
0 0
- android-电话窗口弹出以及LayoutInflater和inflate的用法
- android LayoutInflater和inflate()方法的用法
- android LayoutInflater和inflate()方法的用法
- Android - LayoutInflater和inflate方法的用法
- android LayoutInflater和inflate()方法的用法
- LayoutInflater和inflate的用法
- LayoutInflater和inflate的用法
- LayoutInflater和inflate的用法
- Android成长之路-LayoutInflater和inflate的用法
- Android成长之路-LayoutInflater和inflate的用法
- Android成长之路-LayoutInflater和inflate的用法
- Android成长之路-LayoutInflater和inflate的用法
- Android成长之路-LayoutInflater和inflate的用法
- LayoutInflater、inflate的用法
- android LayoutInflater.inflate()的参数及其用法
- android LayoutInflater.inflate()的参数及其用法
- Android:LayoutInflater的inflate函数用法
- android LayoutInflater.inflate()的参数及其用法
- 调试stvd遇到的问题
- R 语言绘图004-legend()参数详解
- 2015年度APP分类
- Java 多线程之线程间协作
- HLSL bytecode to GLSL编译器的第一步
- android-电话窗口弹出以及LayoutInflater和inflate的用法
- Atom 编辑器快捷键列表汇总整理
- 二分图匹配
- 欢迎使用CSDN-markdown编辑器
- 华为机试——汽水瓶
- Spring AOP动态代理
- [CodeForces 615B]Longtail Hedgehog[DP]
- eclipse最常用的快捷键整理
- Android API Document Download