选择后获取所选项目

时间:2018-03-14 15:06:44

标签: java android

我可以将微调器值设置为列表中的第一个项目。我想从列表中选择一个项目后获取该值。

使用https://jsfiddle.net/zx47xdfm/3/

package com.example.nddudumaguingbsit.atry;

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

public class MainActivity extends AppCompatActivity {
Spinner sp1;
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sp1=(Spinner)findViewById(R.id.spinner1);
    tv1=(TextView)findViewById(R.id.textView);

    sp1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
           sp1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
               @Override
               public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                   tv1.setText("ROFL");
               }
           });

            return false;
        }
    });


}

}

1 个答案:

答案 0 :(得分:0)

你尝试过这样的事吗?

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)
}

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

此处有更多信息和示例:

https://developer.android.com/guide/topics/ui/controls/spinner.html