2012-03-15 48 views
3

我有销售应用程序。当用户点击EditText时,弹出计算器要显示。在我的情况下,当用户双击EditText onClickLister时,它只显示弹出式计算器。的Android的EditText OnClickListener问题

这是myEditText

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); 
       } 
       } 

这是可以转让的问题代表他们想双击每一个产品clcik。

我想显示弹出式计算器,当用户选择的文本编辑。 (不要双击)。

这是我的代码:

 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包含的产品信息 列表中输入值弹出计算器 后数量的用户点击它的时候,它会clearFocus上那行EditText数量&它去第一行EditText'

这是我的屏幕的图片

picture of my screen

+0

图片在哪里?你能否再次改写你的问题,有些部分不是很清楚。 – Mayank 2012-03-28 08:09:47

回答

3

我一直在使用setOnTouchListener

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

谢谢你的提示!我只是想知道为什么我们不应该使用ACTION_DOWN来处理对EditText的点击。是否有使用ACTION_UP的某些原因? – 2014-11-30 12:22:11

6

你为什么加入您的EditText onClickEvent

对于EditText上是有意义的,只要得到的EditText集中使用onFocusChangeListener()将火起来的对话框。在onFocusChangeListener()你可以检查EditText获得焦点或失去焦点

+0

之前,我写了'onFocusChangeListener()'我的代码。问题是当用户误输入3时,所以他想要改变为5,那时我们不能在同一个'EditText' – Piraba 2012-03-15 07:25:39

+0

中再次调用它,你可以确保当你关闭对话框时,你从EditText()中移除焦点,这种方式点击它agin会导致对话框重新出现 – Mayank 2012-03-15 07:28:18

+0

如何从EditText()删除焦点我试过当弹出窗口解雇称为'txtQty.setFocusable(false);'。但不工作 – Piraba 2012-03-15 07:40:48

1

您可以尝试在代码中添加这个(在点击事件):

EditText yourEditText= (EditText) findViewById(R.id.yourEditText); 
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT); 

什么也可能是一个问题,是在XML文件中,也许某个地方, ,它说:<requestFocus />。 删除此

+0

我动态地创建了EditText。我也试过这个。无效 – Piraba 2012-03-15 07:31:56

0

如果我正确理解你的问题,你打算“做某事”,当用户“双击”的EditText上。

不知道这是最好的解决办法, 这里的样本这样做。 模仿“双击”,由500ms的倒计时。

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(); 
      } 

     } 
    }); 
    } 
1

尝试使用

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

做。如果它就像你从来不想让用户输入是通过键盘值,并希望用户输入对话框中的值。

那么为什么要使用EditText,你可以有一个TextView给它一个适当的背景和文字字体。

然后写onClickListener为那TextView