2012-03-15 49 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