如何使活动产生x个旋转器,x是变量?

时间:2015-04-17 16:17:09

标签: java android android-studio

我正在尝试使用Android Studio制作计算化合物摩尔质量的程序。作为其中的一部分,我有一个下拉菜单,询问复合中有多少元素,并且我试图获得该数字并为用户生成更多的下拉/微调器以选择每个元素。

我目前正在做的是将该数字放入文本视图,以便我可以验证它是否正常工作。不幸的是,我不确定如何更新活动以显示对textview的更改。当我单击微调器中的项目时,它将保持默认文本。

更新:现在我已经解决了这个问题,我怎样才能让那些旋转器产生在第一个之下?基本上我会拿变量保持元素的数量并创建那个数量的微调器。

我很感激任何见解。

这是我的java代码:

    package com.jae.john.molarmasscalculator;

    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.*;
    /*import android.view.Menu;
    import android.view.MenuItem;*/
    import android.widget.*;
    import android.widget.AdapterView.*;
    import android.widget.Spinner;
    /*import android.widget.Spinner;
    import android.widget.ArrayAdapter;
    import android.widget.AdapterView.OnItemSelectedListener;*/


    public class MainActivity extends ActionBarActivity implements OnItemSelectedListener {

        //variables
        TextView tvTest;
        Spinner testSpinner;


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

            //variables
            tvTest = (TextView)findViewById(R.id.testview);
            testSpinner = (Spinner) findViewById(R.id.spinner1);

            testSpinner.setOnItemSelectedListener(this);
        }


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }




       public void onItemSelected(AdapterView<?> parent, View view,
                                   int pos, long id) {
            // An item was selected. You can retrieve the selected item using
            // parent.getItemAtPosition(pos)
            //Spinner spinner1 = (Spinner) findViewById(R.id.spinner);


           String testText = testSpinner.getSelectedItem().toString();
           tvTest.setText(testText);
           //recreate();

        }

        public void onNothingSelected(AdapterView<?> parent) {
            // Another interface callback
        }

    }

我的活动xml:     

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:drawable/btn_dropdown"
        android:spinnerMode="dropdown"
        android:longClickable="false"
        android:layout_below="@+id/textView2"
        android:layout_alignParentStart="true"
        android:clickable="true"
        android:dropDownWidth="match_parent"
        android:entries="@array/numberofelements" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="1. Number of Different Elements"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="2. Select Each Element"
        android:id="@+id/textView"
        android:layout_below="@+id/spinner1"
        android:layout_alignParentStart="true" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:drawable/btn_dropdown"
        android:spinnerMode="dropdown"
        android:longClickable="false"
        android:clickable="true"
        android:dropDownWidth="match_parent"
        android:entries="@array/elements"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView" />

    <Spinner
        android:id="@+id/spinner3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:drawable/btn_dropdown"
        android:spinnerMode="dropdown"
        android:longClickable="false"
        android:clickable="true"
        android:dropDownWidth="match_parent"
        android:entries="@array/elements"
        android:layout_below="@+id/spinner2"
        android:layout_alignParentStart="true" />

    <Spinner
        android:id="@+id/spinner4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:drawable/btn_dropdown"
        android:spinnerMode="dropdown"
        android:longClickable="false"
        android:clickable="true"
        android:dropDownWidth="match_parent"
        android:entries="@array/elements"
        android:layout_below="@+id/spinner3"
        android:layout_alignParentStart="true" />

    <Spinner
        android:id="@+id/spinner5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:drawable/btn_dropdown"
        android:spinnerMode="dropdown"
        android:longClickable="false"
        android:clickable="true"
        android:dropDownWidth="match_parent"
        android:entries="@array/elements"
        android:layout_below="@+id/spinner4"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="test"
        android:id="@+id/testview"
        android:layout_below="@+id/spinner5"
        android:layout_alignParentStart="true" />

</RelativeLayout>

我的数组xml与数组填充第一个微调器:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="numberofelements">
        <item>  1  </item>
        <item>  2  </item>
        <item>  3  </item>
        <item>  4  </item>
        <item>  5  </item>
    </string-array>

    <string-array name="elements">
        <item> </item>
        <item> </item>
    </string-array>

</resources>

1 个答案:

答案 0 :(得分:0)

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

            //variables
            tvTest = (TextView)findViewById(R.id.testview);
            testSpinner = (Spinner) findViewById(R.id.spinner1);

            testSpinner.setOnItemSelectedListener(this);
        }


public void onItemSelected(AdapterView<?> parent, View view,
                                   int pos, long id) {
            // An item was selected. You can retrieve the selected item using
            // parent.getItemAtPosition(pos)
            //Spinner spinner1 = (Spinner) findViewById(R.id.spinner);


           String testText = testSpinner.getSelectedItem().toString();
           tvTest.setText(testText);
           //recreate();

        }

或者,没有实现OnItemSelectedListener:

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

            //variables
            tvTest = (TextView)findViewById(R.id.testview);
            testSpinner = (Spinner) findViewById(R.id.spinner1);


testSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
         String testText = testSpinner.getSelectedItem().toString();
               tvTest.setText(testText);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {
        // your code here
    }

});
}