执行 Onclick 方法时按钮变为空白(文本)

Button becomes blank(text) when Onclick method executed

这是 xml 代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"
android:layout_height="match_parent"     android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text="eBay Search"
    android:textSize="13pt"
    android:textStyle="bold"
    android:textColor="#000099"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">

</TextView>


<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="80dp"
    android:id="@+id/tableLayout">

    <TableRow
        android:layout_marginTop="15dp">
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:text="Keyword:"
            android:textSize="10pt"
            android:layout_weight="2">
        </TextView>

        <EditText
            android:id="@+id/keyword"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            >
        </EditText>
    </TableRow>
    <TableRow
        android:layout_marginTop="15dp">
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:text="Price From:"
            android:textSize="10pt"
            android:layout_weight="2">
        </TextView>

        <EditText
            android:id="@+id/from"
            android:layout_width="0dp"

            android:inputType="numberDecimal"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            >
        </EditText>
    </TableRow>
    <TableRow
        android:layout_marginTop="15dp">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Price To:"
            android:textSize="10pt"
            android:layout_weight="2">
        </TextView>

        <EditText
            android:id="@+id/to"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_weight="3"
            android:inputType="numberDecimal"
            android:layout_width="0dp"
            >
        </EditText>
    </TableRow>
    <TableRow
        android:layout_marginTop="15dp">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Sort By:"
            android:textSize="10pt"
            android:layout_weight="2">
        </TextView>


        <Spinner
            android:id="@+id/sortby"
            android:layout_height="wrap_content"
            android:prompt="@string/op1"
            android:layout_width="fill_parent"
            android:entries="@array/sortlist">
        </Spinner>

    </TableRow>

    <TableRow
        android:layout_marginTop="15dp"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Clear"
            android:id="@+id/Clear"
            android:onClick="clearForm"
            android:layout_marginLeft="50dp"
             />


        <Button
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:text="Search"
            android:textColor="@color/black"
            android:id="@+id/search"
            android:layout_marginLeft="50dp"
            />

        </TableRow>
</TableLayout>

<TextView
    android:id="@+id/keyerror"
    android:layout_marginTop="420dp"
    android:layout_centerHorizontal="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textSize="7pt"
    android:textColor="@color/red"
    >
</TextView>

<TextView
    android:id="@+id/priceerror"
    android:layout_marginTop="445dp"
    android:layout_centerHorizontal="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textSize="7pt"
    android:textColor="@color/red"
    >
</TextView>

这是包含搜索按钮的 onclicklistener 方法的主要活动代码:

package com.example.hrishikesh.hw9;

import android.app.AlertDialog;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import java.net.URLEncoder;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Button search = (Button)findViewById(R.id.search);

    search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


                int error=0;

                EditText text1 = (EditText) findViewById(R.id.keyword);
                EditText text2 = (EditText) findViewById(R.id.from);
                EditText text3 = (EditText) findViewById(R.id.to);
                TextView keyerror = (TextView) findViewById(R.id.keyerror);
                TextView priceerror = (TextView)     findViewById(R.id.priceerror);
                String keyword = text1.getText().toString();
                String from = text2.getText().toString();
                String to = text3.getText().toString();
                String reg = "^[0-9]+$";

                if (keyword == null || keyword.length() < 1 ||     keyword.trim().length() < 1) {

                    keyerror.setText("Enter a valid keyword");
                    error=1;
                }
            else
                {
                    keyerror.setText("");
                    error=0;
                }


            if(to.matches(reg) || to.length()==0)
            {

            }
            else
            {

                priceerror.setText("Enter a valid positive number for maximum price");
                error=1;
            }


            if(from.matches(reg) || from.length()==0)
            {

            }
            else
            {
                priceerror.setText("Enter a valid positive number for minimum price");
                error=1;
            }
            if(from.length()>0 && to.length()>0)
            if(Integer.parseInt(from) - Integer.parseInt(to)>=0){

                priceerror.setText("Minimum price has to be less than     maximum price");
                error=1;
            }


            }




    });


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void clearForm(View v){
    EditText text1 = (EditText)this.findViewById(R.id.keyword);
    EditText text2 = (EditText)this.findViewById(R.id.from);
    EditText text3 = (EditText)this.findViewById(R.id.to);

    if (text1 != null) text1.setText("");
    if (text2 != null) text2.setText("");
    if (text3 != null) text3.setText("");

    Spinner spin1 = (Spinner)this.findViewById(R.id.sortby);

    spin1.setSelection(0);
}


public void validate(View v){



}

public void search(String keywords,int from,int to,String sortorder){
    String url;
    int i=0;
    url =   "http://hrishisaraf-env.elasticbeanstalk.com/";




}
}

每当执行onclick方法并检测到关键字为空等错误时。错误文本已设置,“搜索”按钮变为空白。

不知道为什么,但将宽度设置为 wrap_content 解决了问题。