vendredi 27 mai 2016

If Statement Spinner and VIsibility

im try to create Spinner Condition using visibility and disable checkbox,

i have button, spinner, text, and checkbox.

first operation must Click Button to VISIBLE all Spinner, Text, and Checkbox, but before button click checkbox condition 2 and 3 already display im already set all invisible before button click.

then when im try choose item on spinner if else statement not work, and when im try change another item still checkbox condition 1 still not enable, what wrong with my code?

here String.xml

<resources>
    <string-array name="choice">
        <item>Choose</item>
        <item>Choice 1</item>
        <item>Choice 2</item>
    </string-array>

    <string name="cond1">Condition 1</string>
    <string name="cond2">Condition 2</string>
    <string name="cond3">Condition 3</string>
</resources>

dummy_layout.xml

<ScrollView
    xmlns:android="http://ift.tt/nIICcg"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        xmlns:tools="http://ift.tt/LrGmb4"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.felix.tgpfg.CalculatorFragment">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center_horizontal"
            android:weightSum="1">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Click!"
                        android:id="@+id/clickbtn"
                        android:textSize="15dp"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_margin="10dp">
                <TextView
                    android:id="@+id/SpinnerTitle"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Choose :"
                    android:textSize="18dp"/>
                <Spinner
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/choices"/>

            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Condition"
                android:id="@+id/txtcond"/>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="6.80">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp">
                    <CheckBox
                        android:id="@+id/cond1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/cond1"/>
                    <CheckBox
                        android:id="@+id/cond2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/cond2"/>
                    <CheckBox
                        android:id="@+id/cond3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/cond3"/>
                </LinearLayout>
            </LinearLayout>

        </LinearLayout>
    </LinearLayout>
</ScrollView>

dummy.class

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;

public class dummy  extends Fragment {

    public dummy() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.dummy_layout, container, false);

        Button Click = (Button) rootView.findViewById(R.id.clickbtn);

        final Spinner spinner = (Spinner) rootView.findViewById(R.id.choices);

        final TextView txtcond = (TextView) rootView.findViewById(R.id.txtcond);
        final CheckBox ccond1 = (CheckBox) rootView.findViewById(R.id.cond1);
        final CheckBox ccond2 = (CheckBox) rootView.findViewById(R.id.cond2);
        final CheckBox ccond3 = (CheckBox) rootView.findViewById(R.id.cond3);
        final TextView spinnertitle = (TextView) rootView.findViewById(R.id.SpinnerTitle);

        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getActivity(), R.array.choice, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinner.setAdapter(adapter);

        spinner.setVisibility(View.INVISIBLE);
        spinnertitle.setVisibility(View.INVISIBLE);
        txtcond.setVisibility(View.INVISIBLE);
        ccond1.setVisibility(View.INVISIBLE);
        ccond2.setVisibility(View.INVISIBLE);
        ccond3.setVisibility(View.INVISIBLE);

        Click.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                spinner.setVisibility(View.VISIBLE);
                spinnertitle.setVisibility(View.VISIBLE);
                txtcond.setVisibility(View.VISIBLE);
                ccond1.setVisibility(View.VISIBLE);
                ccond2.setVisibility(View.VISIBLE);
                ccond3.setVisibility(View.VISIBLE);
            }


        });

          Object item = spinner.getSelectedItem();
                if(item.equals("Choice 1")){
                    ccond1.setVisibility(View.VISIBLE);
                    ccond1.setEnabled(true);
                    ccond2.setVisibility(View.INVISIBLE);
                    ccond2.setEnabled(false);
                    ccond3.setVisibility(View.INVISIBLE);
                    ccond3.setEnabled(false);
                }else{
                    ccond1.setVisibility(View.INVISIBLE);
                    ccond1.setEnabled(false);
                    ccond2.setVisibility(View.VISIBLE);
                    ccond2.setEnabled(true);
                    ccond3.setVisibility(View.VISIBLE);
                    ccond3.setEnabled(true);
                }

        // Inflate the layout for this fragment
        return rootView;
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}

First RunningChange Item but Condition of Checkbox still same




Aucun commentaire:

Enregistrer un commentaire