2012-01-05 11 views
2

この問題の解決策を見つけるために検索しました。setOnKeyListenerは、エミュレータと同じようにgalaxy s2で同じ動作をしません。

私はユーザーがeditText(@ + id/incBox)フィールドに数字を入力し、その数字の特定の割合が別のeditText(@ + id/excBox)に自動的に配置されるsimpe android appを持っています。

Iは、入力された番号を取得し、他のフィールド(その逆)を更新する計算を行い、両方のEDITTEXTフィールドのsetOnKeyListenerを実装しています。

このコードは、数字が入力されるたびにエミュレータで正常に機能し、他のeditTextフィールドは更新されます。しかし、私のsamsung galaxy s2でapkを実行すると、他のフィールドは更新されません。電話でを更新するために、他のフィールドについてはあなたはプレスがsoftkeyboardに入力しています。私はここで何が欠けていますか? CANCELイベントやMULTITOUCHイベントがOnKeyリスナーに影響しないようにするため、event.getAction()== KeyEvent.ACTION_UP "if"節を削除しました。これを電話で働かせるにはどうすればいいですか?

私が経験していますもう一つの問題は、そのフィールドに値を入力した後です。他のフィールドに移動し、削除または入力を押すことはかなり遅いです。すでに入力されたフィールドの数字を削除するためにバックスペースボタンを押すと0.5秒の遅延が生じることがあります。これは試合のキャッチのためですか?

ご協力いただければ幸いです。

この

はmain.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:gravity="top" 
    android:background="@drawable/bground" 
> 

    <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:text="@string/inclusive" 
    android:textSize="20px" 
    android:paddingTop="10px" 
    android:textStyle="bold" /> 


    <EditText 
    android:id="@+id/incBox" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:inputType="numberDecimal" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginLeft="25px" 
    android:layout_marginRight="25px" 
    android:singleLine="true" 
    > 
    <requestFocus /> 
    </EditText> 


    <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="20px" 
    android:textStyle="bold" 
    android:gravity="center_horizontal" 
    android:text="@string/exclusive" /> 

    <EditText 
    android:id="@+id/excBox" 
    android:layout_width="fill_parent" 
    android:layout_height="60px" 
    android:inputType="numberDecimal" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginLeft="25px" 
    android:layout_marginRight="25px" 
    android:singleLine="true" 
    > 
    </EditText> 
</LinearLayout> 

であり、ここでactivity.java

public class PercentageCalculatorActivity extends Activity 
{ 

private EditText inclusive; 
private EditText exclusive; 
DecimalFormat cost = new DecimalFormat("0.00"); 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    // Set Activity Layout 
    setContentView(R.layout.main); 

    // Get the EditText and Button References 
    inclusive = (EditText)findViewById(R.id.incBox); 
    exclusive = (EditText)findViewById(R.id.excBox); 

    //Set KeyListener to ourself 
    inclusive.setOnKeyListener(new OnKeyListener() 
    { 
public boolean onKey(View v, int keyCode, KeyEvent event) 
     { 
     try 
     { 

     double num = Double.parseDouble(inclusive.getText().toString()); 

     if (num > 0) 
     {       
      num = num * 0.25; 
     String exc = cost.format(num).toString(); 
     exclusive.setText(exc); 
      } 

    // Close the keyboard on enter press         if (keyCode == 66 )       { 
    InputMethodManager imm = (InputMethodManager)getSystemService (Context.INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(inclusive.getWindowToken(), 0); 
    } 

      return false; 
      } 
      catch (Throwable e) 
       { 
       // set other field to empty if this field is also blank 
    exclusive.setText(""); 
         return false; 
       }    
     } 
    }); 

    exclusive.setOnKeyListener(new OnKeyListener() 
    { 
     public boolean onKey(View v, int keyCode, KeyEvent event) 
     { 
      if(event.getAction()==KeyEvent.ACTION_UP) 
      { 
       try 
       { 
        double num = Double.parseDouble(exclusive.getText().toString()); 

        if (num > 0) 
        { 
         num = num * 4; 
         String exc = cost.format(num).toString(); 
         inclusive.setText(exc); 
        } 

        if (keyCode == 66 ) 
        { 
         InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
         imm.hideSoftInputFromWindow(exclusive.getWindowToken(), 0); 
        }     

      return false; 
       } 
       catch (Throwable e) 
       { 
        // set other field to empty if this field is also blank 
           inclusive.setText(""); 
        return false; 
       } 

      } 
      else 
      { 
       return false; 
      } 

     } 
    }); 

答えて

1

それは実際にデバイスではなく、あなたが選択し、キーボード上にない依存です。私は前にこれにぶつかりました。あなたが別のキーボードのためにキーボードを交換すると、突然それが期待どおりに反応することがわかります。私はこれの原因がわからないが、それを試してみて、それがあなたの問題を解決するかどうか見てみるべきだ。

関連する問題