确定先前是否选择了 EditText 的最佳方法

发布于 2024-10-09 09:21:10 字数 3483 浏览 2 评论 0原文

我目前正在开发一个程序,该程序有两个 EditText 框,一个用于屏幕宽度,一个用于屏幕高度。我想要做的是,当用户知道高度或宽度并且他们将测量值输入到与他们的测量值相对应的 EditText 框中时,当他们按 Enter 时,会给他们未知的尺寸对于 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 语句,但无法使其正常工作。有什么建议吗?

I am currently working on a program that has two EditText boxes one for a screens Width and one for the screens Height. What I want to be able to do is that when a user knows either the height or width and they enter the measurement they have into the EditText box that corresponds with the measurement that they have and when they press enter it will give them the unknown dimension for a 16:9 screen.

Here is the user interface:

<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"
    />

I want the app to be able to give me the value based on the last number entered into the EditText box and so that the user doesn't have to clear both boxes before trying another number. what is the best way of keeping up with which of the two boxes that the user had entered text into last was? I tried a if else statement based on if the boxes were in focus but couldn't manage to get it to work. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满栀 2024-10-16 09:21:10

已编辑
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);
}

EDITED:
Add a TextWatcher(the same one) to both the EditText which has the afterTextChanged(Editable s) method.

Editable s belongs to the textbox that the user is currently entering, and have a member variable of boolean type widthSelected.

So your code will be something like this:

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

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

In your button's 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文