2010-12-28 12 views
0

私は現在、画面幅と画面高さの2つのEditTextボックスを持つプログラムで作業しています。私ができることをしたいのは、ユーザーが高さと幅のどちらかを知っていて、測定値を入力すると、それらが持っている測定値に対応するEditTextボックスに入っていて、Enterキーを押すと、 16:9画面の場合 EditTextが以前に選択されたかどうかを判断する最も良い方法

<TextView android:layout_height="wrap_content" 
    android:text="Screen Ratio:" 
    android:id="@+id/ScreenRatio" 
    android:layout_width="100dip" 
    android:layout_alignTop="@+id/ratio_spinner" 
    android:layout_alignBottom="@+id/ratio_spinner" 
    android:gravity="center_vertical" /> 

<Spinner android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/ScreenRatio" 
    android:id="@+id/ratio_spinner"/> 

<TextView 
    android:layout_height="wrap_content" 
    android:text="Units:" 
    android:layout_width="wrap_content" 
    android:layout_toLeftOf="@+id/unit_spinner" 
    android:layout_alignTop="@+id/unit_spinner" 
    android:layout_alignBottom="@+id/unit_spinner" 
    android:gravity="center_vertical" 
    android:layout_alignParentLeft="true" 
    android:id="@+id/ScreenUnit"/> 

<Spinner android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/ScreenRatio" 
    android:layout_below="@+id/ratio_spinner" 
    android:id="@+id/unit_spinner"/> 

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:paddingTop="3dip" 
    android:paddingBottom="3dip" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/unit_spinner" 
    android:id="@+id/screen_layout" 
    android:stretchColumns="1,2"> 

    <View 
     android:layout_height="2dip" 
     android:background="#FF909090"/> 

    <TableRow> 
     <TextView 
      android:layout_column="1" 
      android:paddingTop="3dip" 
      android:text="Screen Width:"/> 
     <TextView 
      android:layout_column="2" 
      android:padding="3dip" 
      android:text="Screen Height:"/> 
    </TableRow> 

    <TableRow 
    android:paddingBottom="3dip" > 
     <EditText 
      android:layout_column="1" 
      android:id="@+id/width_EditText" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@android:drawable/editbox_background" 
      android:inputType="number" 
      android:textSize="14px" 
      android:hint="Enter a Width"/> 
     <EditText 
      android:layout_column="2" 
      android:id="@+id/height_EditText" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@android:drawable/editbox_background" 
      android:numeric="integer" 
      android:textSize="14px" 
      android:hint="Enter a Height"/> 
    </TableRow> 



</TableLayout> 

<Button 
    android:id="@+id/enter" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="Enter" 
    android:gravity="fill" 
    android:paddingBottom="10dip" 
    android:paddingRight="50dip" 
    android:paddingLeft="50dip" 
    android:layout_alignRight="@+id/screen_layout" 
    android:layout_below="@+id/screen_layout" 
    /> 

私はアプリが最後の数に基づいて、私に価値を与えることができるようにしたいのEditTextボックスに入力されたので、以下は

は、ユーザーインターフェイスがありますユーザーが別の番号を試す前に両方のボックスをクリアする必要はないことを確認します。ユーザーが最後にテキストを入力した2つのボックスのうちのどれを維持するための最良の方法は何ですか?ボックスに焦点が当てられていてもそれを動作させることができない場合、if else文を試しました。助言がありますか?

答えて

0

EDITED
afterTextChanged(Editable s)方法を有するEditText両方に(同じもの)TextWatcherを加えます。

Editable sは、ユーザーが現在入力しているテキストボックスに属し、ブール型widthSelectedというメンバ変数を持っています。

だからあなたのコードは次のようになります:あなたのボタンのonClickListenerで

if(s == edtWidth.getText()) 
{ 
    // User is typing in Width 
     widthSelected = true; 

}else 
{ 
// User is typing in Height 
    widthSelected = false; 
} 

:TextWatcherコードが住んで

if(widthSelected) 
{ 
width = Integer.parseInt(edtWidth.getText().toString()); 
edtHeight.setText("" + (9 * width)/16); 
}else{ 

height = Integer.parseInt(edtHeight.getText().toString()); 
edtWidth.setText("" + (16 * height)/9); 
} 
+0

?私のXMLファイルまたは私の.javaファイルで?どのように私はそれを実装するだろうか?私は本当にジュベとアンドロイドに新しいです。 –

+0

@ mdbella.ua、まず、答えを少し修正しました....もう一つは、TextWatcherはコードの中にあります。コードの中でEditTextの 'addTextChangedListener'関数を使用してください。 Googleの例。 – st0le

関連する問題