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
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);
}
}
浙公网安备 33010602011771号