0

一些背景
我有一个 9 行的 TableLayout。我选择了 TableLayout,因为我可以成功地将 EditText 视图在 1 行上并排排列。正如您将在下面看到的,我在两行上有 6 个 EditText,但为了避免重复,我只包含 6 个中的 2 个以节省空间,并且我删除了一些不必要的行,因为它们显示正确(只是 TextView)。

问题
我遇到的问题是在我按下布局上的按钮后,所有 EditText 视图都消失了。我的按钮启动与 WCF Restful 服务的联系,但使用 AsyncTask 这样做。有一个等待对话框占用用户,有时会弹出吐司消息。这都是正常的行为。为了完美,EditText 在我按下按钮后立即消失。

我用 google 和 stackoverflow 进行了搜索,我看到了类似的问题,但与我遇到的问题不同。非常奇怪的是,这只发生在我在手机上调试时,它不会发生在 AVD 模拟器中。

统计
电话:T-Mobile myTouchQ LG-C800(仅供参考:不是最好的手机)我扎根了。
手机 Android 版本:2.3.4(LG 显然不相信更新)
IDE:IntelliJ 11.1
Android SDK:4.0.3
Java 版本:1.7

问题
1. 我应该使用不同的布局吗?
2. 任何线索为什么会发生这种情况?

这是我的 Layout.xml 的缩短版本:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/tableLayout1"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent" >
    <TableRow android:id="@+id/rowTitleWinningNumbers"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="0dip" >
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Winning Numbers" />
    </TableRow>
    <TableRow android:id="@+id/rowWinningNumbers"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="0dip" >
        <EditText android:id="@+id/txtLotto1"
                  android:inputType="text"
                  android:layout_width="0dp"
                  android:layout_weight="16.67"
                  android:editable="false"
                  android:focusable="false" />
        <EditText android:id="@+id/txtLotto2"
                  android:inputType="text"
                  android:layout_width="0dp"
                  android:layout_weight="16.67"
                  android:editable="false"
                  android:focusable="false" />
    </TableRow>
    <TableRow android:id="@+id/rowTitlePlayerNumbers"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="0dip" >
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Your Numbers" />
    </TableRow>
    <TableRow android:id="@+id/rowPlayerNumbers"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="0dip" >
        <EditText android:id="@+id/txtPlayer1"
                  android:inputType="text"
                  android:layout_width="0dp"
                  android:layout_weight="16.67"
                  android:hint="@string/strDefaultNumber" />
        <EditText android:id="@+id/txtPlayer2"
                  android:inputType="text"
                  android:layout_width="0dp"
                  android:layout_weight="16.67"
                  android:hint="@string/strDefaultNumber" />
    </TableRow>
    <TableRow android:id="@+id/rowPlayerStatus2"
              android:layout_height="wrap_content"
              android:layout_width="wrap_content"
              android:gravity="center">
        <Button android:id="@+id/btnSubmit"
                android:text="Did I Win?!"
                android:onClick="btnSubmit_onClick" />
        <CheckBox android:id="@+id/cbxSavePlayerNumbers"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:gravity="center"
                  android:lines="2"
                  android:text="Save my \nNumbers" />
    </TableRow>
</TableLayout>

编辑评论中要求的代码

public void btnSubmit_onClick(View v)
{
    try
    {
        clearStatuses();

        setLabelText(R.id.lblTimeStamp, DateTime.Today().toString());

        savePlayerNumbers();

        //This method has to perform its main task asynchronously
        getWinningLotteryNumbers();
    }
    catch(Exception ex)
    {
        ex.toString();
    }
}

private void getWinningLotteryNumbers()
{
    try
    {
        _waitDialog = ProgressDialog.show(MyActivity.this, "Working", "Getting latest Lottery drawing, Please Wait...", true);

        getButton(R.id.btnSubmit).setEnabled(false);

        String strUrl = getRString(R.string.strWCFServiceURL);

        new LottoProxy().execute(strUrl);
    }
    catch (Exception e)
    {
        resetControlStates();
    }
}

private void clearStatuses()
{
    TextView lblStatus = getLabel(R.id.lblStatus);
    lblStatus.setText("");
    lblStatus.setTextColor(Color.BLACK);

    for (int i = 0; i < 6; i++)
        resetTextBoxBackResource(getWinningBox(i), getPlayerBox(i));
}

我非常坚持这一点,所以提前感谢您的帮助。这是我的第一个真正的应用程序(而不是只是玩玩),所以如果我犯了一个新手错误,请告诉我。

4

1 回答 1

0

所以事实证明,EditText 并没有消失,而是因为我的 clearStatuses() 方法迭代所有可用的 EditText 并将它们的背景更改为黑色而从视图中隐藏。这不是等待处理此问题,因为显然在执行此操作时您正在覆盖库存的 EditText 背景图像。

令人难以置信的部分是(正如我在上面的评论中所说)我注释掉了 onClick() 方法中的所有代码,无论如何这仍然发生。因此,准确指出问题是相当困难和令人困惑的。我想知道这是否是Android中的错误?

我学会了使用以下代码将您的 EditText 更改回它通常的外观:

yourEditText.setBackgroundResource(android.R.drawable.editbox_background_normal);

我在这里找到了这个答案: Removing the background color of an EditText

一个警告,即使此代码会将 EditText 恢复为它的外观,它可能与原始外观不匹配。我这么说是因为那是我所经历的,但这对我来说已经足够了。

于 2012-08-25T15:59:08.913 回答