Android中的PopupWindow
1.功能
PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的,可以设置显示位置。
2.需求
弹出软键盘,实现键盘功能从而实现金额的标准显示
3.界面布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keypad_dialog"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="4">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/btn1"
android:layout_weight="1"
android:soundEffectsEnabled="true"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2"
android:id="@+id/btn2"
android:layout_weight="1"
android:soundEffectsEnabled="true"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3"
android:id="@+id/btn3"
android:layout_weight="1"
android:soundEffectsEnabled="true"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消"
android:id="@+id/btn_cancel"
android:layout_weight="1"
android:soundEffectsEnabled="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="4">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4"
android:id="@+id/btn4"
android:layout_weight="1"
android:soundEffectsEnabled="true"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5"
android:id="@+id/btn5"
android:layout_weight="1"
android:soundEffectsEnabled="true"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6"
android:id="@+id/btn6"
android:layout_weight="1"
android:soundEffectsEnabled="true"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除"
android:id="@+id/btn_delete"
android:layout_weight="1"
android:soundEffectsEnabled="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="7"
android:id="@+id/btn7"
android:layout_weight="1"
android:soundEffectsEnabled="true"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="000"
android:id="@+id/btn000"
android:layout_weight="1"
android:soundEffectsEnabled="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="8"
android:id="@+id/btn8"
android:layout_weight="1"
android:soundEffectsEnabled="true"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="00"
android:id="@+id/btn00"
android:layout_weight="1"
android:soundEffectsEnabled="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="9"
android:id="@+id/btn9"
android:layout_weight="1"
android:soundEffectsEnabled="true"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/btn0"
android:layout_weight="1"
android:soundEffectsEnabled="true"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="确定"
android:background="#00FF00"
android:id="@+id/btn_enter"
android:soundEffectsEnabled="true"/> </LinearLayout>
</LinearLayout
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAK8AAABfCAIAAAARYgA7AAALcElEQVR4nO3d61MT5xoA8OcP6ufOaWfsHKcHL0G5xFhqMAKSEQ4EAQ1KgoEkpJWUagLhJtEarqKgpJVLVYLcFMIlhFx2QxLoSQX1qMVpDLn14vmwdEF7phM2md1Q3meeL354Xx/3/bnJu/vsBsIoUITDBoPB6/UC8Qe/3/+G3v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Go0NGuokEqbm5snJibS0tJ2o4bExMQ5k0lcVnb16tV/JSRgOJ6RkXHnzp2kpCQMxxMSEixWKw0acnJy9N9+e+XKFZlcnpKSMjM7SywMzRqUSmWtRjNkMGRmZu5GDV9fuvT555+zWKyZ2dkzZ89mZmWx2Ww7hqWnpxO/mBD5VNFo6Lp5Mzk5OTExcXBwUKVSpaWlsVis7T5jE72GhyMjLBYrNTVVp9PtaA3U9xTzZjP55WDebCYuI9oxbLvPFES5p1iwWMjzkHlhIfK3rsdQA7GikX84/g01xCrRDhNpQBriUQO6T4E0IA1Iw19rYKqDfonxDvo4eAd93Gnw+XwLFss87UH8MkI4HA6FQjiO01+A0+kkNXg8HvoLsFgsgUAgvjSEw+FgMOinPcJbIhQK0V/Ae6+Tpb+AYDAYjo93yaKe6XgJpAHFZvz4y4/NvzYzm0gDis1AHfQoNgNpQLEZhIb/AVn04r0vPLNoAAAAAElFTkSuQmCC" alt="" />
4.实现KeyPadDialog类以及键盘功能
为了显示金额,限定长度为12
public class KeyPadDialog extends PopupWindow implements View.OnClickListener{
private View parent;
private String str = "";
private Handler handler;
public KeyPadDialog(Context context, View parent,Handler hand){
super(context);
this.parent = parent;
handler = hand; //set content view
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = layoutInflater.inflate(R.layout.keyboard, null);
setContentView(contentView); //set width and height
setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); //在触摸到外部区域后也不会消失
setFocusable(false);
setTouchable(true);
setOutsideTouchable(false); contentView.findViewById(R.id.btn1).setOnClickListener(this);
contentView.findViewById(R.id.btn2).setOnClickListener(this);
contentView.findViewById(R.id.btn3).setOnClickListener(this);
contentView.findViewById(R.id.btn4).setOnClickListener(this);
contentView.findViewById(R.id.btn5).setOnClickListener(this);
contentView.findViewById(R.id.btn6).setOnClickListener(this);
contentView.findViewById(R.id.btn7).setOnClickListener(this);
contentView.findViewById(R.id.btn8).setOnClickListener(this);
contentView.findViewById(R.id.btn9).setOnClickListener(this);
contentView.findViewById(R.id.btn0).setOnClickListener(this);
contentView.findViewById(R.id.btn00).setOnClickListener(this);
contentView.findViewById(R.id.btn000).setOnClickListener(this);
contentView.findViewById(R.id.btn_cancel).setOnClickListener(this);
contentView.findViewById(R.id.btn_delete).setOnClickListener(this);
contentView.findViewById(R.id.btn_enter).setOnClickListener(this);
} public void show(){
// Show at bottom of parent
this.showAtLocation(parent, Gravity.BOTTOM,0,0);
Log.d("DialogTest","ShowDialog");
} @Override
public void onClick(View v){
boolean isCancel = false;
switch (v.getId()){
case R.id.btn1:
if(str.length() < 12){
str = str + String.valueOf(1);
}
break;
case R.id.btn2:
if(str.length() < 12){
str = str + String.valueOf(2);
}
break;
case R.id.btn3:
if(str.length() < 12){
str = str + String.valueOf(3);
}
break;
case R.id.btn4:
if(str.length() < 12){
str = str + String.valueOf(4);
}
break;
case R.id.btn5:
if(str.length() < 12){
str = str + String.valueOf(5);
}
break;
case R.id.btn6:
if(str.length() < 12){
str = str + String.valueOf(6);
}
break;
case R.id.btn7:
if(str.length() < 12){
str = str + String.valueOf(7);
}
break;
case R.id.btn8:
if(str.length() < 12){
str = str + String.valueOf(8);
}
break;
case R.id.btn9:
if(str.length() < 12){
str = str + String.valueOf(9);
}
break;
case R.id.btn0:
if ((str.length() != 0) && (str.length() < 12)) {
str = str + "0";
}
break;
case R.id.btn00:
if ((str.length() != 0) && (str.length() < 11)) {
str = str + "00";
}
break;
case R.id.btn000:
if ((str.length() != 0) && (str.length() < 10)) {
str = str + "000";
}
break;
case R.id.btn_cancel:
str = "";
break;
case R.id.btn_delete:
if (str.length() != 0) {
str = str.substring(0,str.length()-1);
}
break;
case R.id.btn_enter:
Message msg = new Message().obtain();
msg.what = 2;
handler.sendMessage(msg);
break;
default:
break;
}
Message msg = new Message().obtain();
msg.what = 1;
msg.obj = str;
handler.sendMessage(msg);
}
}
5.实现MainActivity
必须在主界面加载完后才能弹出软键盘
public class MainActivity extends Activity implements View.OnClickListener{
private View mView;
KeyPadDialog dialog;
private EditText editMoney;
private Button button_scan;
private Button button_card;
private String money = ""; private Handler handler = new Handler(){
public void handleMessage(Message msg){
switch (msg.what){
case 0:
Toast.makeText(MainActivity.this,msg.obj.toString(),Toast.LENGTH_LONG).show();
break;
case 1:
updateMoney(msg.obj.toString());
break;
case 2:
if (money.length() != 0 ){
Intent intent = new Intent(MainActivity.this,SaleActivity.class);
Bundle bundle = new Bundle();
bundle.putCharSequence("money",money);
intent.putExtras(bundle);
startActivity(intent);
}
break;
default:
break;
}
};
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = layoutInflater.inflate(
R.layout.activity_main, null);
setContentView(mView); editMoney = (EditText)findViewById(R.id.inputmoney);
editMoney.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
dialog = new KeyPadDialog(MainActivity.this,mView,handler);
dialog.show();
return true;
}
}); button_scan = (Button)findViewById(R.id.btn_scan);
button_card = (Button)findViewById(R.id.btn_card);
button_scan.setOnClickListener(this);
button_card.setOnClickListener(this);
} @Override
public void onClick(View v){
switch (v.getId()){
case R.id.btn_scan:
break;
case R.id.btn_card:
break;
default:
break;
}
} //金额的标准显示(带“,”和两位小数)
public void updateMoney(String str){
if (str.length() == 0){
editMoney.setText("$0.00");
return;
}
if(str.length() > 12){
return;
}
if (str.length() == 1){
money = "$"+"0.0" + str.substring(0);
}
if (str.length() == 2){
money = "$"+"0." + str.substring(0,str.length());
}
if ((str.length() > 2) && (str.length() <= 5)){
money = "$"+str.substring(0,str.length()-2) + "." + str.substring(str.length()-2);
}
if ((str.length() > 5) && (str.length() <= 8)){
money = "$"+str.substring(0,str.length()-5) +","+str.substring(str.length()-5,str.length()-2) +"." + str.substring(str.length()-2);
}
if ((str.length() > 8) && (str.length() <= 11)){
money = "$"+str.substring(0,str.length()-8) +","+str.substring(str.length()-8,str.length()-5)+","+str.substring(str.length()-5,str.length()-2) +"." + str.substring(str.length()-2);
}
if (str.length() == 12){
money = "$"+str.substring(0,1) +","+str.substring(1,str.length()-8) +","+str.substring(str.length()-8,str.length()-5)+","+str.substring(str.length()-5,str.length()-2) +"." + str.substring(str.length()-2);
}
editMoney.setText(money);
}
}
Android中的PopupWindow的更多相关文章
- android中使用PopupWindow实现弹出窗口菜单
结合上篇android中使用ViewPager实现图片拖动,我们实现了点击“帮助”按钮的功能,这一篇则是接着上一篇,让我们一起来完成“我的”按钮的功能,这一功能,则是使用PopupWindow来实现弹 ...
- Android中的PopupWindow详解
Android的对话框有两种:PopupWindow和AlertDialog.它们的不同点在于: AlertDialog的位置固定,而PopupWindow的位置可以随意 AlertDialog是 ...
- Android中的PopupWindow的使用
PopupWindow 需要一个自定义的布局文件 列如:popupwindow.xml <?xml version="1.0" encoding="utf-8&qu ...
- Android 中PopupWindow使用 (转)
参考学习后遇到问题: 要引用:有好几个,可以用错误提示解决: import android.widget.PopupWindow; import android.widget.Toast; Activ ...
- Android中Popupwindow和Dialog的区别
Android中的对话框有两种:PopupWindow和AlertDialog.它们都可以实现弹窗功能,但是他们之间有一些差别,下面总结了一点. (1)Popupwindow在显示之前一定要设置宽高, ...
- Android开发之PopupWindow
/* * Android开发之PopupWindow * * Created on: 2011-8-8 * Author: blueeagle * Email: liujiaxiang@g ...
- Android 7.0 PopupWindow 的兼容问题
Android7.0 PopupWindow的兼容问题 Android7.0 中对 PopupWindow 这个常用的控件又做了一些改动,修复了以前遗留的一些问题的同时貌似又引入了一些问题,本文 ...
- Android中那些有你不知道的事
在安卓开发中,总有那么一些看似简单,实则绊脚的难题,等你去探索,等你去解决,也许你已经遇见了解决了,也许你还没碰上,写下这篇总结,希望能帮助那行即将遇到的朋友,快速解决这些小问题! 一.activit ...
- Android中相机和相冊使用分析
Android中相机和相冊使用分析 欢迎转载,但请尊重原创(文章来自不易,转载请标明转载出处,谢谢) 在手机应用程序中,使用自带的相机拍照以及相冊选择喜欢的图片是最常见只是的用户需求,那么怎么合理使用 ...
随机推荐
- php 验证码
$im =imagecreate(500,500); $bak =imagecolorallocate($im,200,100,0); $shk = imagecolorallocate($im,0, ...
- iOS 键盘类型
版权声明:本文为博主原创文章.请尊重作者劳动成果,转载请注明出处. UIKeyboardTypeDefault: UIKeyboardTypeASCIICapable: UIKeyboardTypeN ...
- java验证码刷新
可以使用a标签,这样省事 <img id="code_img" src="validcode" alt="登录验证码" /> & ...
- C/C++入门基础---指针(2)
5,数组指针的不同含义 int a[5][10]; printf(%d, %d, %d\n", a, a+1, &a+1); //1310392,1310432,1310592 a ...
- [题解]UVa 11082 Matrix Decompressing
开始眨眼一看怎么也不像是网络流的一道题,再怎么看也觉得像是搜索.不过虽然这道题数据范围很小,但也不至于搜索也是可以随随便便就可以过的.(不过这道题应该是special judge,因为一题可以多解而且 ...
- Arguments Optional
function add() { //return false; if(typeof arguments[0] !== "number" || arguments.length & ...
- ABAP 读取EXCEL文件到内表
1.选择excel文件: PARAMETERS: P_FILE LIKE RLGRAP-FILENAME OBLIGATORY. AT SELECTION-SCREEN ON VALUE-REQUES ...
- SQL 计算两个地理坐标相差的距离的函数
Create function getGreatCircleDistance(@lat1 decimal(18,11),@lng1 decimal(18,11),@lat2 decimal(18,11 ...
- io流(详询请加qq:2085920154)
import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class ioTest ...
- mysql日志 解析
mysql有4种不同的日志,分别是二进制日志,查询日志,慢查询日志和错误日志,这些日记记录着数据库工作的方方面面,可以帮助我们了解数据库的不同方面的踪迹,下面先介绍二进制日志的作用和使用方法,并利用二 ...