在textView android中动态设置文本

时间:2017-09-20 13:06:50

标签: android radio-button android-spinner radio-group

我的android活动中有单选按钮组和微调器。首先,我选择一个单选按钮,然后选择微调项目。我想在textView中看到选定的微调项目。但是,当我更改微调项目时,textView不会更新。它仅在我更改单选按钮时更新。如何动态更新textView?我正在尝试制作我的单选按钮和微调器并在textView上显示。

public class GenelBagis extends AppCompatActivity  {
    Spinner spinnerGenelBagis;
    ArrayAdapter<String> dataAdapterForGenelBagis;
    String[] aylar;
    TextView mText;
    int amount=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_genel_bagis);

        aylar = getResources().getStringArray(R.array.month);
        spinnerGenelBagis = (Spinner)findViewById(R.id.spinner2);
        dataAdapterForGenelBagis = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,aylar);
        spinnerGenelBagis.setAdapter(dataAdapterForGenelBagis);
        mText = (TextView)findViewById(R.id.textView16);






        spinnerGenelBagis.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    RadioButton meslekLisesiButton = (RadioButton) findViewById(R.id.meslekLisesi);
                    RadioButton yuksekOgrenimButton = (RadioButton) findViewById(R.id.yuksekOgrenim);
                    RadioButton yuksekLisansButton = (RadioButton) findViewById(R.id.meslekLisesi);
                    RadioButton doktoraButton = (RadioButton) findViewById(R.id.meslekLisesi);

                String text = spinnerGenelBagis.getSelectedItem().toString();
                int month = Integer.parseInt(text);

                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {



                        if (meslekLisesiButton.isChecked() == true) {
                           String amount = Integer.toString(month * 200);
                           mText.setText(amount);
                        } else if (yuksekOgrenimButton.isChecked() == true) {
                            //
                        } else if (yuksekLisansButton.isChecked() == true) {
                            //
                        } else if (doktoraButton.isChecked() == true) {
                            //

                        }

                    }
                });
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });




    }}

xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.merve.tev.GenelBagis"
    tools:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp">



    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:text="Burs Vermek İstediğiniz Toplam Tutar"
        tools:layout_constraintTop_creator="1"
        android:layout_marginTop="22dp"
        app:layout_constraintTop_toBottomOf="@+id/spinner2"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="16dp" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="279dp"
        android:layout_height="130dp"
        android:checkedButton="@+id/meslekLisesi"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintBottom_creator="1"
        android:layout_marginStart="16dp"
        app:layout_constraintBottom_toBottomOf="@+id/spinner2"
        android:layout_marginTop="92dp"
        tools:layout_constraintLeft_creator="1"
        android:layout_marginBottom="92dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="16dp">

        <RadioButton
            android:id="@+id/meslekLisesi"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Meslek Lisesi (Aylık) 200 TL" />

        <RadioButton
            android:id="@+id/yuksekOgrenim"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"

            android:text="Yüksek Öğrenim (Aylık) 510 TL"
            tools:layout_editor_absoluteX="42dp"
            tools:layout_editor_absoluteY="169dp" />



        <RadioButton
            android:id="@+id/yuksekLisans"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"

            android:text="Yüksek Lisans (Aylık) 850 TL" />

        <RadioButton
            android:id="@+id/doktora"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"

            android:text="Doktora (Aylık) 1150 TL" />

    </RadioGroup>

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="310dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        tools:layout_constraintTop_creator="1"
        android:layout_marginTop="26dp"
        app:layout_constraintTop_toBottomOf="@+id/textView15"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="16dp" />

    <Button
        android:id="@+id/devamEt2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAllCaps="false"
        android:textSize="15sp"
        android:text="Devam Et"
        style="@style/Widget.AppCompat.Button.Colored"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintBottom_creator="1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="@+id/textView16"
        tools:layout_constraintLeft_creator="1"
        android:layout_marginBottom="43dp"
        app:layout_constraintLeft_toLeftOf="@+id/textView16" />

    <TextView
        android:id="@+id/textView13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Öğrenci Eğitim Düzeyine Göre Bağış"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintBottom_creator="1"
        app:layout_constraintBottom_toTopOf="@+id/radioGroup1"
        android:layout_marginStart="16dp"
        android:layout_marginTop="46dp"
        tools:layout_constraintLeft_creator="1"
        android:layout_marginBottom="29dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="16dp" />

    <TextView
        android:id="@+id/textView15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Kaç Aylık Burs Vermek İstersiniz?"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintBottom_creator="1"
        android:layout_marginStart="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="16dp" />

    <TextView
        android:id="@+id/textView16"
        android:layout_width="89dp"
        android:layout_height="29dp"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintBottom_creator="1"
        android:layout_marginStart="16dp"
        app:layout_constraintBottom_toBottomOf="@+id/devamEt2"
        android:layout_marginTop="64dp"
        app:layout_constraintTop_toBottomOf="@+id/spinner2"
        tools:layout_constraintLeft_creator="1"
        android:layout_marginBottom="61dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="16dp" />


</android.support.constraint.ConstraintLayout>

我的活动如下: activity

1 个答案:

答案 0 :(得分:1)

我完全改变了方法。看一看。

public class Test extends AppCompatActivity {
 Spinner spinnerGenelBagis;
 RadioGroup radioGroup;
 ArrayAdapter<String> dataAdapterForGenelBagis;
 String[] aylar;
 TextView mText;

 private static int spinnerValue = 0;
 private static int radioValue = 0;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_genel_bagis);

    aylar = getResources().getStringArray(R.array.month);
    spinnerGenelBagis = (Spinner) findViewById(R.id.spinner2);
    dataAdapterForGenelBagis = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, aylar);
    spinnerGenelBagis.setAdapter(dataAdapterForGenelBagis);
    mText = (TextView) findViewById(R.id.textView16);
    radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {


            if (checkedId == R.id.meslekLisesi) {

                radioValue = 200;
                updateTextView();

            } else if (checkedId == R.id.yuksekOgrenim) {

                radioValue = 300;
                updateTextView();

            } else if (checkedId == R.id.yuksekLisans) {
                //
            } else if (checkedId == R.id.doktora) {
                //

            }

        }
    });

    spinnerGenelBagis.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {


        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

            String text = spinnerGenelBagis.getSelectedItem().toString();
            spinnerValue = Integer.parseInt(text);
            updateTextView();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
}

private void updateTextView() {
    String totalAmount = Integer.toString(spinnerValue * radioValue);
    mText.setText(totalAmount);
}

}

希望它有所帮助。
而且,如果您使用它,则无需编写yuksekOgrenimButton.isChecked() = trueyuksekOgrenimButton.isChecked()自己返回布尔值。