简体   繁体   中英

Java android xml textbox has no text = no button hit

i have a problem here. I need it so if there is no text in Either of my 2 textboxes that you wont beable to hit the button

my JAVA code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.teachme);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    Button teach = (Button)findViewById(R.id.btn_teach_send);
    teach.setOnClickListener(this);


}

@Override
public void onClick(View v) {
    switch(v.getId())
    {

        case R.id.btn_teach_send:
        {
            // Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://monaiz.net/get.php");

            String responseStr = "";

            try {
                TextView word = (TextView)findViewById(R.id.tv_teach_request);
                TextView answer = (TextView)findViewById(R.id.tv_teach_response);

                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
                nameValuePairs.add(new BasicNameValuePair("word", word.getText().toString()));
                nameValuePairs.add(new BasicNameValuePair("answer", answer.getText().toString()));
                nameValuePairs.add(new BasicNameValuePair("action", "teach"));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity( );

                responseStr = EntityUtils.toString( entity );

            } catch (Exception e) {
                // TODO Auto-generated catch block
            }

            if( responseStr.equals("ok") )
            {
                Toast.makeText(getApplicationContext(), "Poka just learned a new word!", Toast.LENGTH_LONG).show();
                try {
                    this.finish();
                } catch (Throwable e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}
}

And then this is my xml code which is obv for the design of the app..

the button and the edit text stuff is in there

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"  >

    <ScrollView android:scrollbars="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fillViewport="true" android:isScrollContainer="true">
        <LinearLayout android:orientation="vertical" android:id="@+id/ll_teach" android:background="#ffffffff" android:layout_width="fill_parent" android:layout_height="fill_parent">
            <LinearLayout android:gravity="center_horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10.0dip" android:layout_marginBottom="2.0dip" android:layout_weight="0.0">
                <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="50.0dip" android:src="@drawable/if_ask" />
            </LinearLayout>
            <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0">

                    <EditText android:gravity="top|left|center" android:maxLength="30" android:id="@+id/tv_teach_request" android:background="@drawable/mespeak" android:paddingLeft="23.0dip" android:paddingTop="6.0dip" android:paddingRight="28.0dip" android:paddingBottom="23.0dip" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginLeft="20.0dip" android:layout_marginRight="5.0dip" android:layout_alignParentTop="true" style="@style/TeachBubbleFont" />

            </LinearLayout>
            <LinearLayout android:gravity="center_horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10.0dip" android:layout_marginBottom="2.0dip" android:layout_weight="0.0">
                <ImageView android:layout_gravity="left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="40.0dip" android:src="@drawable/then_response" />
            </LinearLayout>
            <LinearLayout android:layout_gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0">
                    <EditText android:gravity="top|left|center" android:maxLength="30" android:id="@+id/tv_teach_response" android:background="@drawable/pokaspeak" android:paddingLeft="28.0dip" android:paddingTop="6.0dip" android:paddingRight="23.0dip" android:paddingBottom="23.0dip" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginLeft="1.0dip" android:layout_marginRight="20.0dip" android:layout_alignParentTop="true" android:layout_alignParentRight="true" style="@style/TeachBubbleFont" />
            </LinearLayout>
            <RelativeLayout android:gravity="center_vertical" android:id="@+id/RelativeLayoutBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0">
                <Button android:id="@+id/btn_teach_send" android:layout_width="wrap_content" android:layout_height="35.0dip" android:text="@string/btn_teach_send" android:layout_centerInParent="true" />
            </RelativeLayout>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

You just need to setenabled property of the button for this. If two textbox has no text then disable button and if they have text then enable button.

Here's example

String str1, str2;

str1 = word.getText().toString();
str2 = answer.getText().toString();

if(!(str1.equals("")) && !(str2.equals("")))
{
  teach.setEnabled(true);
}
else
{
  teach.setEnabled(false);
}

EDIT

If you want to check as soon as any of edittext get changed then you need to use textchangelistner for that.

Here I made small example for it. It enables button only when 2 edittext have any of text. Hope this will help you.

public class TSActivity extends Activity {

     String str = "";
     String str1 = "";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Button btn = (Button)findViewById(R.id.btn);
        TextView txt = (TextView)findViewById(R.id.text);
        TextView txt1 = (TextView)findViewById(R.id.text1);
        btn.setEnabled(false);


        txt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

                str = s.toString();
                if( !(str.equals("")) && !(str1.equals("")) )
                {
                    btn.setEnabled(true);
                }
                else
                {
                    btn.setEnabled(false);
                }
            }
        });
 /**************************************************************************************************/       
        txt1.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub              
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub              
            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

                str1 = s.toString();
                if( !(str.equals("")) && !(str1.equals("")) )
                {
                    btn.setEnabled(true);
                }
                else
                {
                    btn.setEnabled(false);
                }
            }
        });
    }  
}

Thanks...

将从哪个来源设置edittext值,这取决于是否从特定侦听器中的任何操作获取了edittext值,请调用上面的函数;如果您从任何函数调用中设置的edittext值使该函数使该函数的返回类型为boolean根据true或false在oncreate中调用上述步骤。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM