在按钮单击时禁用文本字段并同时启用另一个按钮

时间:2016-06-29 10:50:22

标签: java android android-studio

我想:

我的文字字段textInput& textInput2btnClickMe已停用 &安培;当我点击按钮button2时启用Bevestig weddenschap

我无法弄清楚如何做到这一点。你能救我吗?

XML代码:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/details_frankrijk"
    android:textSize="17sp"
    android:id="@+id/tvDetails"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/positie"
    android:id="@+id/lblOutput"
    android:layout_below="@+id/tvDetails"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="28dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/txtInput"
    android:layout_below="@+id/lblOutput"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignRight="@+id/lblOutput"
    android:layout_alignEnd="@+id/lblOutput" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/bedrag"
    android:id="@+id/lblOutput2"
    android:layout_below="@+id/txtInput"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/txtInput2"
    android:layout_below="@+id/lblOutput2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignRight="@+id/lblOutput2"
    android:layout_alignEnd="@+id/lblOutput2" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/bevestig_weddenschap"
    android:id="@+id/button"
    android:layout_below="@+id/plaatsbet"
    android:layout_toRightOf="@+id/tvDetails"
    android:layout_toEndOf="@+id/tvDetails"
    android:onClick="btnClickMe" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/plaats_de_weddenschap"
    android:id="@+id/plaatsbet"
    android:layout_below="@+id/txtInput2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/volgende"
    android:id="@+id/button2"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:clickable="false" />

Java代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tweedescherm);
    TextView tvDetails = (TextView) findViewById(R.id.tvDetails);

    Intent i = getIntent();
    //haal data van de intents en zet deze in de textview
    tvDetails.setText(i.getIntExtra("details", R.string.details_frankrijk));

}

public void btnClickMe (View v) {
    TextView tvDetails = (TextView) findViewById(R.id.tvDetails);
    EditText txtInput = (EditText) findViewById(R.id.txtInput);
    TextView lblOutput = (TextView) findViewById(R.id.lblOutput);
    EditText txtInput2 = (EditText) findViewById(R.id.txtInput2);

    lblOutput.setText("Het land " + tvDetails.getText().toString() + " haalt in het EK volgens jou positie " + txtInput.getText().toString() + ". Je hebt hiervoor " + txtInput2.getText().toString() + " euro ingezet.");

}

}

2 个答案:

答案 0 :(得分:0)

您可以.setEnabled(false)使用EditText.setClickable(false)使用Button,并在Button Bevestig weddenschap

 txtInput.setEnabled(false);
 txtInput2.setEnabled(false);
 button.setEnabled(false);

点击 Bevestig weddenschap 时点击布尔值

 txtInput.setEnabled(true);
 txtInput2.setEnabled(true);
 button.setEnabled(true);

编辑首先创建一个自定义函数

 private void setDisableOrEnable(boolean flag) {
   txtInput.setEnabled(flag);
   txtInput2.setEnabled(flag);
   button.setEnabled(flag);
 }

第一次onCreate()调用函数setDisableOrEnable(false) 并在 Bevestig weddenschap 按钮上点击通话setDisableOrEnable(true)

并确保在txtInput

上调用txtInput2时初始化buttonsetDisableOrEnable(false)onCreate()

所以你的onCreate()看起来像这样。

  EditText txtInput;
  TextView lblOutput; 
  EditText txtInput2;
  TextView tvDetails;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_tweedescherm);
  tvDetails = (TextView) findViewById(R.id.tvDetails);
  txtInput = (EditText) findViewById(R.id.txtInput);
  lblOutput = (TextView) findViewById(R.id.lblOutput);
  txtInput2 = (EditText) findViewById(R.id.txtInput2);

  setDisableOrEnable(false);

  Intent i = getIntent();
  //haal data van de intents en zet deze in de textview
  tvDetails.setText("" + i.getIntExtra("details", R.string.details_frankrijk));

  }


public void btnClickMe (View v) {

    setDisableOrEnable(true);
    lblOutput.setText("Het land " + tvDetails.getText().toString() + " haalt in het EK volgens jou positie " + txtInput.getText().toString() + ". Je hebt hiervoor " + txtInput2.getText().toString() + " euro ingezet.");

 }

 private void setDisableOrEnable(boolean flag) {
   txtInput.setEnabled(flag);
   txtInput2.setEnabled(flag);
   button.setEnabled(flag);
 }

希望这有帮助

答案 1 :(得分:0)

对于EditText,请将editText.setEnabled(false);editText.setFocusable(false);

结合使用

仅限按钮button.setEnable(false)就足够了。