Android EditText 在按下 TableLayout 中的按钮后消失



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

问题所在
我遇到的问题是,在我按下布局上的按钮后,所有 EditText 视图都消失了。"我的按钮"启动与 WCF Restful Service 的联系,但使用 AsyncTask 执行此操作。有一个等待对话框来占用用户,有时会弹出 toast 消息。这都是正常行为。为了完美,编辑文本在我按下按钮后立即消失。

用谷歌和stackoverflow搜索过,我看到了类似的问题,但不是我遇到的问题。非常奇怪的是,这仅在我在手机上调试时发生,而不会在AVD模拟器中发生。

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

问题
1. 我应该使用不同的布局吗?
2. 有什么线索为什么会这样吗?

这是我的布局的缩短版本.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));
}

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

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

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

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

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

我在这里找到了这个答案:删除编辑文本的背景色

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

最新更新