Android中的PopupWindow

时间:2024-01-06 11:44:32

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,iVBORw0KGgoAAAANSUhEUgAAAK8AAABfCAIAAAARYgA7AAALcElEQVR4nO3d61MT5xoA8OcP6ufOaWfsHKcHL0G5xFhqMAKSEQ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ra6urlIq09LSDMPDRcXFbe3tNGuoq6uTy+XdPT15+fm7UcO+/fstVmtFRUVDY+Onn36K4XhOTs7169fZR45gOL7/wIEFi4UGDZmZmX39/dd1ugsSSeKhQxarVaFQ1Go0NGuokEqbm5snJibS0tJ2o4bExMQ5k0lcVnb16tV/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);
  }
}