【问题标题】:To enable and disable a button in android [duplicate]在android中启用和禁用按钮[重复]
【发布时间】:2014-08-28 07:12:50
【问题描述】:

我想创建一个医疗应用。因此,在该人选择“左眼”选项后,测试应该开始。它应该显示结果。在测试结果结束时,应显示测试另一只眼睛的选项。那时应该禁用“左眼”按钮选项。反之亦然,即如果该人选择“右眼”,则在测试后,应禁用右眼选项。我附上代码。

public void resetView(){

    setContentView(R.layout.duochrometest);
    setObjects();
    int a[] = { R.id.leftEye, R.id.rightEye };
    setOnClickListener(a);
}

@Override
public void onClick(View v) {

    switch (v.getId()) {
        case R.id.leftEye:
        case R.id.rightEye:
            resetTestView();
            break;                          
        case R.id.testagain:
            ++testCount;
            resetView();
            break;
        case R.id.backtoTests:
            backToTests();
            break;
        default:
            calculateResult((String) v.getTag());
            changeObject();
    }
}

xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    android:orientation="vertical"
    android:background="@drawable/bg">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:padding="8dp"
        android:gravity="top"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/duochromeDesc"
        android:textColor="@color/white"/>

        <LinearLayout  
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:padding="2dp"
        android:orientation="horizontal">

        <Button android:id="@+id/leftEye" android:text="@string/lefteye"   android:textColor="@color/white" android:layout_width="150dp"  android:layout_height="wrap_content" android:background="@drawable/borderlightgray"></Button>

        <Button android:id="@+id/rightEye" android:text="@string/righteye" android:textColor="@color/white" android:layout_width="150dp" android:layout_height="wrap_content" android:background="@drawable/borderlightgray"></Button>

        </LinearLayout>
</LinearLayout>

请帮帮我。

【问题讨论】:

    标签: android button


    【解决方案1】:

    您可以在按钮上调用 setEnabled(false)。

    【讨论】:

    • 我应该把上面的语句放在我的代码哪里??
    • 你需要声明你的按钮,例如Button button = (Button) findViewById(R.id.button);button.setEnabled(false);
    【解决方案2】:

    在布局文件的按钮内部使用它。 android:visibility="gone" 并在要显示按钮的活动中使用此标记 Button.setVisibility(View.VISIBLE); 有效地使用这两个标志“消失”和“可见”你可能会得到答案。 #拉文德拉

    【讨论】:

      猜你喜欢
      • 2018-07-15
      • 1970-01-01
      • 2016-03-20
      • 2015-06-20
      • 2012-09-20
      • 2014-09-27
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      相关资源
      最近更新 更多