天天看点

android按钮点击属性值,android – 将按钮可点击属性设置为false

我需要在

Android中禁用按钮的点击事件.就像样本一样,我尝试过以下操作.我把TextView命名为(输入文本)作为Name.条件检查TextView是否为空按钮且可点击是否应设置为false.但是,打印Toast时不会发生这种情况.有人可以告诉我原因.此外,如果文本字段不为空,我想将按钮的可点击事件重置为true.

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文件:

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"/>

android:text="TextView"

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

android:text="Button"

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>