我在這裏有一個問題。 我需要它,所以如果有我的2個文本框在任何一種沒有文字,你不會比布爾按下按鈕Java android xml textbox has no text = no button hit
我的Java代碼:
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();
}
}
}
}
}
}
然後這是被OBV我的XML代碼該應用程序的設計..
按鈕和編輯文本的東西在裏面
<?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>
可能存在一些問題,在xml中您使用的是編輯文本,而在java文件中則是採取文本視圖。除此之外,不能僅僅通過getText()方法檢查相應的編輯文本是否爲空。並將按鈕設置爲setClickable(false)..... – Android 2012-03-20 06:19:25