作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我这里有个问题。我需要它,所以如果我的两个文本框中没有任何文本,您将无法点击按钮
我的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();
}
}
}
}
}
}
然后这是我的 xml 代码,它是应用程序设计的 obv..
按钮和编辑文本内容就在那里
<?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>
最佳答案
您只需为此设置按钮的enabled 属性。如果两个文本框没有文本,则禁用按钮,如果有文本,则启用按钮。
这里是例子
String str1, str2;
str1 = word.getText().toString();
str2 = answer.getText().toString();
if(!(str1.equals("")) && !(str2.equals("")))
{
teach.setEnabled(true);
}
else
{
teach.setEnabled(false);
}
编辑
如果您想在任何编辑文本发生更改时立即进行检查,那么您需要使用 textchangelistner。
这里我为它做了一个小例子。仅当 2 个 edittext 包含任何文本时才启用按钮。希望这会对您有所帮助。
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);
}
}
});
}
}
谢谢...
关于Java android xml文本框没有文本=没有按钮点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9782043/
我是一名优秀的程序员,十分优秀!