如何将微调器选择的值传递给同一 activity 中的 onCreate() 部分?

how to pass spinner selected value to onCreate() section in the same activity?

我想制作一个基于一些 Edittext 和两个微调器收集数据的应用程序。然后,通过单击按钮 在 TextView 中显示所有字符串数据。我知道这可能很简单,但我想知道如何在微调器中获取选定的项目并将它们显示在其余值附近。 '''

Spinner shift, permit;

EditText TextHere, TrainNumber, SignalPerson, NavganPerson, DriverPerson;

TextView BigText;

Button SaveButton;

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

    shift=(Spinner)findViewById(R.id.shift_spinner);
    permit=(Spinner)findViewById(R.id.permission_spinner);
    TextHere=(EditText)findViewById(R.id.text_here);
    TrainNumber=(EditText)findViewById(R.id.train_number);
    SignalPerson=(EditText)findViewById(R.id.signal_person);
    NavganPerson=(EditText)findViewById(R.id.navgan_person);
    DriverPerson=(EditText)findViewById(R.id.driver_person);
    SaveButton=(Button) findViewById(R.id.save_btn);
    BigText=(TextView)findViewById(R.id.text_text);

    ArrayAdapter ShiftSelect = ArrayAdapter.createFromResource(
            this,
            R.array.shifts,
            R.layout.support_simple_spinner_dropdown_item
    );
    shift.setAdapter(ShiftSelect);
    shift.setOnItemSelectedListener(this);

    ArrayAdapter PermissionSelect = ArrayAdapter.createFromResource(
            this,
            R.array.permissions,
            R.layout.support_simple_spinner_dropdown_item
    );
    permit.setAdapter(PermissionSelect);
    permit.setOnItemSelectedListener(this);

    final String TN = TrainNumber.getText().toString();

    final String TH = TextHere.getText().toString();

    final String SP = SignalPerson.getText().toString();

    final String NP = NavganPerson.getText().toString();

    final String DP = DriverPerson.getText().toString();

    SaveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

}


@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    Spinner spinner = (Spinner) parent;

    if (spinner.getId()==R.id.shift_spinner){
        String SHIFT = parent.getSelectedItem().toString();
    }
    if (spinner.getId()==R.id.permission_spinner){
        String PER = parent.getSelectedItem().toString();
    }
}

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

'''

我想将从 public void onItemSelected 中选择的 Item 传递给 onCreate 方法部分以在 txetview 中显示它。 谢谢。

希望这就是您要的

Spinner shift, permit;

EditText TextHere, TrainNumber, SignalPerson, NavganPerson, DriverPerson;

TextView BigText;

Button SaveButton;
String SHIFT,PER;

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

    shift=(Spinner)findViewById(R.id.shift_spinner);
    permit=(Spinner)findViewById(R.id.permission_spinner);
    TextHere=(EditText)findViewById(R.id.text_here);
    TrainNumber=(EditText)findViewById(R.id.train_number);
    SignalPerson=(EditText)findViewById(R.id.signal_person);
    NavganPerson=(EditText)findViewById(R.id.navgan_person);
    DriverPerson=(EditText)findViewById(R.id.driver_person);
    SaveButton=(Button) findViewById(R.id.save_btn);
    BigText=(TextView)findViewById(R.id.text_text);

    ArrayAdapter ShiftSelect = ArrayAdapter.createFromResource(
            this,
            R.array.shifts,
            R.layout.support_simple_spinner_dropdown_item
    );
    shift.setAdapter(ShiftSelect);
    shift.setOnItemSelectedListener(this);

    ArrayAdapter PermissionSelect = ArrayAdapter.createFromResource(
            this,
            R.array.permissions,
            R.layout.support_simple_spinner_dropdown_item
    );
    permit.setAdapter(PermissionSelect);
    permit.setOnItemSelectedListener(this);

    final String TN = TrainNumber.getText().toString();

    final String TH = TextHere.getText().toString();

    final String SP = SignalPerson.getText().toString();

    final String NP = NavganPerson.getText().toString();

    final String DP = DriverPerson.getText().toString();

    SaveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            BigText.setText(SHIFT+" "+PER);
        }
    });

}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    Spinner spinner = (Spinner) parent;

    if (spinner.getId()==R.id.shift_spinner){
        SHIFT = parent.getSelectedItem().toString();
    }
    if (spinner.getId()==R.id.permission_spinner){
        PER = parent.getSelectedItem().toString();
    }
}

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

试试这个。

TextView textView;
Spinner spinner;

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

    spinner=(Spinner)findViewById(R.id.shift_spinner);
    textView=(TextView)findViewById(R.id.textView);

    // Create adapter here
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);

}


@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    String sel_val = spinner.getSelectedItem().toString();
    // Setting selected value to textView.
    textView.setText(sel_val);

}

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

试试这个。

TextView textView;
Spinner spinner;
String SEL_VALUE;

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

    spinner=(Spinner)findViewById(R.id.shift_spinner);
    textView=(TextView)findViewById(R.id.textView);
    SEL_VALUE="";

    // Create adapter here
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);

}


@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    String sel_val = spinner.getSelectedItem().toString();
    // Setting selected value to textView.
    SEL_VALUE = sel_val;
    textView.setText(sel_val);

}

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