如何将微调器值设置为textView

时间:2017-04-20 17:53:04

标签: android buttonclick

我是编程新手,请原谅任何错误。 我试图分配一个微调器值,该值被选中给TextView但是每件事都出错了。

的OnCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    adapter=ArrayAdapter.createFromResource(this,R.array.grades, android.R.layout.simple_spinner_item);
    grades=(Spinner)findViewById(R.id.spinner);
    grades.setAdapter(adapter);
    grades.setOnItemSelectedListener(MainActivity.this);
}

onSelectesItem Listner

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    TextView spinner_text=(TextView) view;
    Toast.makeText(this,"your grade is " + spinner_text.getText(),Toast.LENGTH_SHORT).show();

   textView.setText(grades.getSelectedItem().toString());//this line should do its work but didnt goes well 
}

2 个答案:

答案 0 :(得分:0)

onCreate()方法中,您在TextView设置findViewById(R.id.textView)onItemSelected()未使用text获取textView的引用{1}}它会导致应用程序崩溃。

onCreate()

中添加以下行
textView = (TextView) findViewById(R.id.YOUR_TEXTVIEW_ID);

以下是完整的工作代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements Spinner.OnItemSelectedListener {

    ArrayAdapter adapter;
    Spinner grades;
    TextView textView;

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

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

        adapter = ArrayAdapter.createFromResource(this, R.array.grades, android.R.layout.simple_spinner_item);

        grades.setAdapter(adapter);
        grades.setOnItemSelectedListener(MainActivity .this);
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        TextView spinner_text = (TextView) view;
        Toast.makeText(this, "Your grade is " + spinner_text.getText(), Toast.LENGTH_SHORT).show();

        textView.setText(grades.getSelectedItem().toString());
    }

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

    }
}

<强>输出:

enter image description here

希望这会有所帮助〜

答案 1 :(得分:0)

您可以通过以下方式获取所选的微调器值:

 @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String selectedString;
        //1: From the Adapter
        selectedString = adapter.getItem(position).toString();

        //2: From the spinner
        selectedString = grades.getSelectedItem().toString();

        //3: From the view 
        TextView spinner_text = (TextView) view;
        selectedString = spinner_text.getText().toString();

        //All of them works just fine choose any one method
        textView.setText(selectedString);
    }