我需要在Android中禁用click-event
按钮。就像一个样本,我尝试了以下几点。我已将TextView
命名为(输入文本)名称。该条件检查TextView
是否为空按钮,并且可单击应设置为false。然而,当吐司印刷时这不会发生。有人可以告诉我原因。此外,如果文本字段不是空的,我想重置clickable event
按钮为true。将按钮可点击属性设置为false
Java文件:
public class ButtonclickabkeActivity extends Activity {
TextView tv;
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView1);
tv.setText("Name");
btn = (Button) findViewById(R.id.button1);
if (tv.getText().toString().length() != 0) {
btn.setClickable(false);
Toast.makeText(getApplicationContext(), "" + tv.getText().toString().length(), Toast.LENGTH_LONG).show();
} else {
btn.setClickable(true);
}
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Button clicked", Toast.LENGTH_LONG).show();
}
});
}
}
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">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
<TextView
android:text="TextView"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Button"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
谢谢我设置btRegsiter.setVisibility(View.VISIBLE); – 2016-06-20 05:31:31
'LinearLayout'上的'setClickable(false)'不会导致子视图无法正常工作。点击时,onClickListener始终会触发。即使在XML'可点击:假'不工作。我错过了什么? – FARID 2017-06-03 12:11:09