天天看點

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"/>