本文介绍了吐司没有出现在我的第二个旋转器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我必须制作两个下拉列表,所以在代码中添加了两个微调器。当我选择第一个微调器吐司显示所选项目但是在选择第二个微调器吐司消息后没有显示。请帮助。 package listdisplay.example.com.spinnertest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import static android.R.attr.country; import static android.widget.Toast.makeText; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener { TextView tv; Spinner微调器; Spinner spinner2; 字符串 [] country = { 印度, China, 巴基斯坦, America}; 字符串 [] city = { 孟买, 德里, Noida, Vashi}; String [] countrydummy; @覆盖 受保护 void onCreate(Bundle savedInstanceState){ super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv =(TextView)findViewById(R.id.textView); spinner =(Spinner)findViewById(R.id.spinner); spinner2 =(Spinner)findViewById(R.id.spinner2); spinner.setOnItemSelectedListener( this ); ArrayAdapter< String> countryad = new ArrayAdapter< String>( this ,android.R.layout.simple_spinner_item,country); spinner.setAdapter(countryad); countrydummy = country; ArrayAdapter< String> cityad = new ArrayAdapter< String>( this ,android.R.layout.simple_spinner_item,city); spinner2.setAdapter(cityad); } @覆盖 public void onItemSelected(AdapterView<?> parent,View view, int i, long id){ // i for位置和l为id Spinner spin1 =(Spinner)parent; Spinner spin2 =(Spinner)parent; // 第一个微调器选择 if (spin1.getId()== R.id.spinner) { String item = parent.getItemAtPosition( ⅰ)的ToString(); Toast t1 = makeText(getApplicationContext(), 选中的项目: + item,Toast .LENGTH_SHORT); t1.show(); } // 第二个微调器选择 if (spin2.getId()== R.id.spinner2) { String item = parent.getItemAtPosition(i).toString(); Toast t2 = makeText(getApplicationContext(), 选中的项目: + item,Toast .LENGTH_SHORT); t2.setGravity(Gravity.CENTER, 0 , 0 ); t2.show(); } } @ Override public void onNothingSelected(AdapterView<?> adapterView){ } } 我的尝试: i在onItemSelected函数中声明了两个微调器,但toast未在第二个微调器中显示解决方案 我注意到你没有为spinner2设置setOnItemSelectedListener。 I have to make two dropdown list so added two spinners in code.when I have selected first spinner toast shows selected item but after selecting second spinner toast message is not showing .Please help.package listdisplay.example.com.spinnertest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import static android.R.attr.country; import static android.widget.Toast.makeText; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener { TextView tv; Spinner spinner; Spinner spinner2; String[] country = {"India","China","Pakistan","America"}; String[] city = {"Mumbai","Delhi","Noida","Vashi"}; String[] countrydummy; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv=(TextView)findViewById(R.id.textView); spinner = (Spinner)findViewById(R.id.spinner); spinner2=(Spinner)findViewById(R.id.spinner2); spinner.setOnItemSelectedListener(this); ArrayAdapter<String> countryad=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,country); spinner.setAdapter(countryad); countrydummy=country; ArrayAdapter<String> cityad=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,city); spinner2.setAdapter(cityad); } @Override public void onItemSelected(AdapterView<?> parent, View view, int i, long id) { // i for position and l for id Spinner spin1= (Spinner)parent; Spinner spin2= (Spinner)parent; //First spinner selection if(spin1.getId() == R.id.spinner) { String item = parent.getItemAtPosition(i).toString(); Toast t1=makeText(getApplicationContext(),"item selected : "+item,Toast.LENGTH_SHORT); t1.show(); } //second spinner selection if(spin2.getId() == R.id.spinner2) { String item = parent.getItemAtPosition(i).toString(); Toast t2=makeText(getApplicationContext(),"item selected : "+item,Toast.LENGTH_SHORT); t2.setGravity(Gravity.CENTER, 0, 0); t2.show(); } } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }What I have tried:i have declared two spinner in onItemSelected function but toast is not showing in second spinner 解决方案 I noticed you have not setOnItemSelectedListener for spinner2. 这篇关于吐司没有出现在我的第二个旋转器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 17:30