不同的布局与微调器

时间:2016-01-06 08:54:01

标签: android-layout android-spinner

当用户从微调器中选择series2时,我想显示不同的布局,当他从微调器中选择其他项时,我想显示不同的布局。 怎么办呢?

这是我的代码:

public class MainActivity extends Activity {
    Button button;
    EditText editText
    EditText editText2;
    TextView textView, textView3;
    int num1;
    int num2;
    int j = 0;
    int s;
    int c;
    Spinner spinner,spinner1;

    String spinner_name_data;

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

            setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.textView);
        editText = (EditText) findViewById(R.id.editText);
        editText2 = (EditText) findViewById(R.id.editText2);
        button = (Button) findViewById(R.id.button);
        textView3 = (TextView) findViewById(R.id.textView3);


        spinner = (Spinner) findViewById(R.id.spinner1);
       final String[]  test = new String[]{"series1", "series2", "series3"};

        ArrayAdapter<String> gameKindArray = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, test);
        gameKindArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(gameKindArray);

        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> arg0, View arg1, int index, long arg3) {

                spinner_name_data = spinner.getSelectedItem().toString();

                if (spinner_name_data.equals(test[0]))

                {


                    textView3.setText(Html.fromHtml("1+x<sup><small>2</small></sup>+x<sup><small>3</small></sup>+x<sup><small>4</small></sup>+x<sup><small>5</small></sup>+.........+x<sup><small>n</small></sup>"));
                } else if (spinner_name_data.equals(test[1]))

                {
                    setContentView(R.layout.layout2);
                    textView = (TextView) findViewById(R.id.textView);
                    editText = (EditText) findViewById(R.id.editText);
                    editText2 = (EditText) findViewById(R.id.editText2);
                    button = (Button) findViewById(R.id.button);
                    textView3 = (TextView) findViewById(R.id.textView3);

                    spinner1 = (Spinner) findViewById(R.id.spinner1);
                    final String[] test = new String[]{"series1", "series2", "series3"};

                    ArrayAdapter<String> gameKindArray = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, test);
                    gameKindArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    spinner.setAdapter(gameKindArray);

                    textView3.setText(Html.fromHtml("0+1+1+2+3+5+8+....n times"));
                } else {
                    textView3.setText(Html.fromHtml("1+1/x+x<sup><small>2</small></sup>+1/x<sup><small>3</small></sup>+x<sup><small>4</small></sup>+1/x<sup><small>5</small></sup>+.....+x<sup><small>n</small></sup>"));
                }
            }

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

            }
        });




                button.setOnClickListener(new View.OnClickListener()

                {
                    public void onClick(View v) {

                        spinner_name_data = spinner.getSelectedItem().toString();

                        if (spinner_name_data.equals(test[0])) {
                            String strNum1 = editText.getText().toString().trim();
                            String strNum2 = editText2.getText().toString().trim();
                            if (strNum1.length() == 0) {
                                Toast.makeText(MainActivity.this, "Please enter X", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            if (strNum2.length() == 0) {
                                Toast.makeText(MainActivity.this, "Please enter N", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            num1 = Integer.parseInt(strNum1);

                            num2 = Integer.parseInt(strNum2);

                            int c = 1, t1, t2 = 0;

                            for (int i = 1; i < num1; i++) {
                                int t = c;
                                c = c * num2;
                                t1 = t + c;
                                t2 = t1 + t2;
                            }
                            j = t2 + c + 1;

                            j = j / 2;


                            textView.setText(String.valueOf(j));
                        }else if (spinner_name_data.equals(test[1])){


                            String strNum1 = editText.getText().toString().trim();

                            if (strNum1.length() == 0) {
                                Toast.makeText(MainActivity.this, "Please enter n", Toast.LENGTH_SHORT).show();
                                return;
                            }


                            num1 = Integer.parseInt(strNum1);



                            int a=0,b=1;
                            for(int i=1;i<num1;i++){

                               c=a+b;
                                a=b;
                                b=c;





                            }



                            textView.setText(String.valueOf(c));





                        }else{

                            String strNum1 = editText.getText().toString().trim();
                            String strNum2 = editText2.getText().toString().trim();
                            if (strNum1.length() == 0) {
                                Toast.makeText(MainActivity.this, "Please enter X", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            if (strNum2.length() == 0) {
                                Toast.makeText(MainActivity.this, "Please enter N", Toast.LENGTH_SHORT).show();
                                return;
                            }



                        }

                    }
                });

    }
}

0 个答案:

没有答案