Лучший способ выяснить, был ли ранее выбран EditText - PullRequest
0 голосов
/ 28 декабря 2010

В настоящее время я работаю над программой, которая имеет два поля EditText, одно для ширины экранов и одно для высоты экранов. Что я хочу сделать, так это то, что когда пользователь знает высоту или ширину и вводит свои измерения в поле EditText, которое соответствует измерению, которое они имеют, и когда они нажимают, введите, оно даст им неизвестное измерение для экрана 16: 9.

Вот пользовательский интерфейс:

<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, и чтобы пользователю не приходилось очищать оба поля, прежде чем пытаться ввести другой номер. Каков наилучший способ узнать, какой из двух полей, в которые пользователь вводил текст в последний раз, был? Я попробовал оператор if else, основанный на том, были ли поля в фокусе, но не смог заставить его работать. Есть предложения?

1 Ответ

0 голосов
/ 28 декабря 2010

EDITED :
Добавьте TextWatcher (один и тот же) к обоим EditText, который имеет метод afterTextChanged(Editable s).

Editable s принадлежит текстовому полю, которое вводит пользователь, и имеет переменную-член логического типа widthSelected.

Таким образом, ваш код будет выглядеть примерно так:

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

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

В вашемКнопка onClickListener:

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);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...