安卓 EditText 点击事件问题

3

我有一个销售应用程序。当用户点击EditText时,弹出计算器应该显示。在我的情况下,只有当用户双击EditText时,它才会显示弹出式计算器。

这是我的EditText

    final EditText txtQty = new EditText(this);
            txtQty.setHeight(1);
            if(productList.get(i).getQty() != 0.00){
                txtQty.setText(Double.toString(productList.get(i).getQty()));
            }
            txtQty.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,42));
            txtQty.setTextSize(9);
            txtQty.setId(i);
            txtQty.setHint("0");
            txtQty.setClickable(true);
            txtQty.setSelected(true);
            txtQty.setSelectAllOnFocus(true);
            txtQty.setInputType(InputType.TYPE_NULL);
            tr.addView(txtQty); 

这是我的代码:

   txtQty.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    String productCode= txtCode.getText().toString();
                    double price = getProductPrice(productCode).getPrice();
                    txtPrice.setText(""+price);

                    if(invPriceEdit.equals("3")){ 
                        if(editPrice.getText().toString().equals("") || editPrice.getText().toString().equals("0.00") || editPrice.getText().toString().equals("0") || editPrice.getText().toString().equals("0.0")){
                            txtPrice.setText(""+ price);
                            editPrice.setText("" +price);
                        }else{
                            String ePrice = editPrice.getText().toString();
                            editPrice.setText("" +ePrice);
                        }
                    }


                        keyAmount = new StringBuffer();
                        if(keyAmount.length() > 0){
                            keyAmount.delete(0, keyAmount.length());
                        }

                        int[] origin = new int[2];
                        v.getLocationOnScreen(origin);
                        final int xVal = origin[0];
                        final int yVal = origin[1] ;

                        dialog = new Dialog(SalesActivityGroup.group.getParent());
                        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

                        View vLoad = LayoutInflater.from(SalesActivityGroup.group.getParent()).inflate(R.layout.key_pad, null);
                        dialog.setContentView(vLoad);
                        android.view.WindowManager.LayoutParams lp= dialog.getWindow().getAttributes();  

                        dialog.setCancelable(true);
                        dialog.setCanceledOnTouchOutside(true);  
                        lp.x = xVal;
                        lp.y = yVal;
                        lp.width = LayoutParams.WRAP_CONTENT;
                        lp.height = LayoutParams.WRAP_CONTENT;
                        lp.gravity = Gravity.TOP | Gravity.LEFT;
                        lp.dimAmount = 0;            
                        dialog.getWindow().setAttributes(lp);
                        dialog.setCancelable(false);
                        keyamDisplay  = (TextView)dialog.findViewById(R.id.keyamDisplay);

                        Button  txtone = (Button)dialog.findViewById(R.id.txtone);
                        txtone.setOnClickListener(new OnClickListener() {
                           public void onClick(View v) {
                              keyAmount.append("1");
                              keyamDisplay.setText("" + keyAmount.toString());

                           }
                        });

                        Button  txttwo = (Button)dialog.findViewById(R.id.txttwo);
                        txttwo.setOnClickListener(new OnClickListener() {
                           public void onClick(View v) {
                              keyAmount.append("2");
                              keyamDisplay.setText("" + keyAmount.toString());

                           }
                        });

                        Button  txtthree = (Button)dialog.findViewById(R.id.txtthree);
                        txtthree.setOnClickListener(new OnClickListener() {
                           public void onClick(View v) {
                              keyAmount.append("3");
                              keyamDisplay.setText("" + keyAmount.toString());
                           }
                        });

                        Button  txtfour = (Button)dialog.findViewById(R.id.txtfour);
                        txtfour.setOnClickListener(new OnClickListener() {
                           public void onClick(View v) {
                              keyAmount.append("4");
                              keyamDisplay.setText("" + keyAmount.toString());
                           }
                        });

                        Button  txtfive = (Button)dialog.findViewById(R.id.txtfive);
                        txtfive.setOnClickListener(new OnClickListener() {
                           public void onClick(View v) {
                              keyAmount.append("5");
                              keyamDisplay.setText("" + keyAmount.toString());
                           }
                        });

                        Button  txtsix = (Button)dialog.findViewById(R.id.txtsix);
                        txtsix.setOnClickListener(new OnClickListener() {
                           public void onClick(View v) {
                              keyAmount.append("6");
                              keyamDisplay.setText("" + keyAmount.toString());
                           }
                         });

                        Button  txtseven = (Button)dialog.findViewById(R.id.txtseven);
                        txtseven.setOnClickListener(new OnClickListener() {
                           public void onClick(View v) {
                              keyAmount.append("7");
                              keyamDisplay.setText("" + keyAmount.toString());
                           }
                        });

                        Button  txteight = (Button)dialog.findViewById(R.id.txteight);
                        txteight.setOnClickListener(new OnClickListener() {
                           public void onClick(View v) {
                              keyAmount.append("8");
                              keyamDisplay.setText("" + keyAmount.toString());
                           }
                        });

                        Button  txtnine = (Button)dialog.findViewById(R.id.txtnine);
                        txtnine.setOnClickListener(new OnClickListener() {
                           public void onClick(View v) {
                              keyAmount.append("9");
                              keyamDisplay.setText("" + keyAmount.toString());
                           }
                        });

                        Button  txtZero = (Button)dialog.findViewById(R.id.txtZero);
                        txtZero.setOnClickListener(new OnClickListener() {
                           public void onClick(View v) {
                              keyAmount.append("0");
                              keyamDisplay.setText("" + keyAmount.toString());
                           }
                        });

                        Button  txtdot = (Button)dialog.findViewById(R.id.txtdot);
                        txtdot.setEnabled(false);
                        txtdot.setOnClickListener(new OnClickListener() {
                               public void onClick(View v) {
                                  keyAmount.append(".");
                                  keyamDisplay.setText("" + keyAmount.toString());
                               }
                        });

                        Button  diaDelete = (Button)dialog.findViewById(R.id.diaDelete);
                        diaDelete.setOnClickListener(new OnClickListener() {
                           public void onClick(View v) {
                             if(keyAmount.length() > 0){
                                keyAmount.delete(keyAmount.length()-1, keyAmount.length());
                            }
                                keyamDisplay.setText("" + keyAmount.toString());
                           }
                        });

                        ImageButton imageSmileExit = (ImageButton)dialog.findViewById(R.id.imageSmileExit);
                        imageSmileExit.setOnClickListener(new OnClickListener() {
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });

                        Button  txtDialogOK = (Button)dialog.findViewById(R.id.txtDialogOK);
                        txtDialogOK.setOnClickListener(new OnClickListener() {
                           public void onClick(View v) {
                               dialog.dismiss();
                         }

                      });
                    dialog.show();
                    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
                }
                }

这是一个销售代表的问题。他们想要双击每个产品单击。

我希望在用户选择文本编辑器时显示弹出式计算器。(不想要双击)。

这是我的代码:

       final EditText txtQty = new EditText(this);
            txtQty.setHeight(1);
            if(productList.get(i).getQty() != 0.00){
                txtQty.setText(Double.toString(productList.get(i).getQty()));
            }
            txtQty.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,42));
            txtQty.setTextSize(9);
            txtQty.setId(i);
            txtQty.setHint("0");
            txtQty.setInputType(InputType.TYPE_NULL);
            tr.addView(txtQty); 

问题是:

`TableLayout 包含产品信息列表,当用户点击 Qty 时,弹出计算器,输入值后,它将清除该行 EditText qty 的焦点,并跳转到第一行 EditText'

这是我的屏幕截图

picture of my screen


请问图片在哪里?您能否重新描述一下您的问题,因为有些部分不是很清晰。 - Mayank
6个回答

7

我已经使用了setOnTouchListener。

    disValEdit.setOnTouchListener(new OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {
                    if(event.getAction() == MotionEvent.ACTION_UP) { } });

谢谢提示!我只是想知道为什么我们不应该使用ACTION_DOWN来处理EditText上的点击。使用ACTION_UP有特定的原因吗? - droide_91

6

为什么要在您的EditText上添加onClickEvent

对于EditText,使用onFocusChangeListener()是有意义的,它会在edittext获得焦点时启动对话框。在onFocusChangeListener()中,您可以检查EditText是否正在获得焦点或失去焦点


之前我在 onFocusChangeListener() 中编写了我的代码。问题是,当用户错误地输入3时,他想要更改为5,这时我们无法再次在同一个 EditText 中调用它。 - Piraba
你可以确保在关闭对话框时从EditText()中移除焦点,这样再次点击它将导致对话框重新出现。 - Mayank
如何从EditText()中移除焦点。我尝试了当弹出窗口关闭时调用txtQty.setFocusable(false);。但是没有起作用。 - Piraba
另一种思考方式是当您的对话框打开时,您的EditText失去焦点,而对话框获得焦点。我不确定通过退出对话框后,EditText是否会自动获得焦点。 - Mayank
@Frankenstein 是的,这没问题。问题是:TableLayout 包含产品 binfo 列表,当用户点击 Qty 时会弹出计算器,输入值后,它将设置并清除该行 EditText qty,当我清除特定行焦点时,它会将焦点转移到第一行 EditText - Piraba

2
如果您不想让用户通过键盘输入值,并希望用户从对话框中输入值,则可以使用TextView,给它一个适当的背景和文本字体。然后为该TextView编写onClickListener。不需要使用EditText

1

尝试使用

txtQty.setEditable(false);
txtQty.setFocusable(false);

1
您可以尝试在代码中添加以下内容(在点击事件中):
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);

还有可能会出问题的是,在您的XML文件中的某个地方, 它说:<requestFocus />。 请删除它。


我动态创建了EditText。我也尝试过这个。但是没有起作用。 - Piraba

0
如果我理解你的问题正确,你想要在用户“双击”EditText时执行某项操作。
不确定这是否是最好的解决方案, 这里有一个示例可以实现。通过500毫秒倒计时来模拟“双击”操作。
private EditText ed1;
private CountDownTimer timer;
private Boolean isWaiting = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ed1 = (EditText) findViewById(R.id.editText1);
    timer = new CountDownTimer(500, 500) {


        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onFinish() {
            isWaiting = false;

        }
    };

    ed1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            if(isWaiting) {
                isWaiting = false;
                timer.cancel();
                // TODO: Pop Up Calculator
                Toast.makeText(getApplicationContext(), "Pop", Toast.LENGTH_LONG).show();
            } else {
                isWaiting = true;
                timer.start();
            }

        }
    });
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接