0

I would like to have:

my Textfields textInput & textInput2 and btnClickMe disabled & button2 enabled when i click on the button Bevestig weddenschap.

I cannot figure out how to do this. Can you help me out?

XML code:

<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 Code:

@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.");

}

}

Sagar Nayak
  • 2,138
  • 2
  • 19
  • 52
  • Use the `View.setEnabled(boolean)` for each of them – Tim Jun 29 '16 at 10:54
  • The link above answers for buttons, but it is the same for textviews, since the method used comes from the View class and the both inherit from it – Tim Jun 29 '16 at 10:56

2 Answers2

0

You can use .setEnabled(false) for the EditTexts and .setClickable(false) for the Button and toggle the boolean when the Button Bevestig weddenschap is clicked

like

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

and when the Bevestig weddenschap is clicked toggle the boolean by

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

EDIT first wirte a custom function

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

first time on onCreate() call the function setDisableOrEnable(false) and on the Bevestig weddenschap button click call setDisableOrEnable(true)

and be sure txtInput , txtInput2 and button is initialized befor calling the setDisableOrEnable(false) on onCreate()

So your onCreate() will look something like this.

  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);
 }

Hope this helps

Sanoop Surendran
  • 3,484
  • 4
  • 28
  • 49
0

For EditText use editText.setEnabled(false); in combination with editText.setFocusable(false);

For button only button.setEnable(false) will suffice.

Talha Mir
  • 1,228
  • 11
  • 19