Android按钮禁用并启用

时间:2014-06-12 19:31:47

标签: android

您好我有一个Android应用程序我想创建类似锁定按钮以防止错误单击应用程序中的任何按钮我使用此方法它很好地禁用所有按钮我喜欢但我无法重新启用它在此之后,我需要的是当我再次按下锁定按钮时再次启用button2和按钮3

这是活动代码

import android.support.v7.app.ActionBarActivity;
import android.app.ActionBar;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
public class AboutActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        addListenerOnButton();
}
    Button button;
    Button button2;
    Button button3;
    private void addListenerOnButton() {
        // TODO Auto-generated method stub
        button = (Button) findViewById(R.id.lock);

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
              button2.setEnabled(false);  //button which u want to disable
              button3.setEnabled(false);  //button which u want to disable
            }
        });
        button2 = (Button) findViewById(R.id.button2);

        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

            }
        });
        button3 = (Button) findViewById(R.id.button3);

        button3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.about, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

这是布局代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.lock.AboutActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/lock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:text="Button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button2"
        android:layout_below="@+id/button2"
        android:layout_marginTop="66dp"
        android:enabled="true"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/lock"
        android:layout_centerVertical="true"
        android:enabled="true"
        android:text="Button" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:3)

您可以使用isEnabled()并使用&#34;而不是&#34;操作员锁定和解锁。如果未启用,则启用,反之亦然。

button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
              button2.setEnabled(!button2.isEnabled());  //button which u want to disable
              button3.setEnabled(!button3.isEnabled());  //button which u want to disable
            }
        });