我用a Spinner和a 创建了Android应用程序TextView.我的任务是在TextView的Spinner下拉列表中显示所选项目.我在onCreate方法中实现了Spinner,所以当我运行程序时,它会显示一个值TextView(在从下拉列表中选择一个项目之前).
我只想在从下拉列表中选择一个项目后才在TextView中显示该值.我该怎么做呢?
这是我的代码:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class GPACal01Activity extends Activity implements OnItemSelectedListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.noOfSubjects);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.noofsubjects_array, android.R.layout.simple_spinner_item);
// Specify the layout …Run Code Online (Sandbox Code Playgroud) 有没有人知道如何在实例化布局时阻止onItemSelected()(OnItemSelectedListener接口)方法运行?我需要知道是否有办法做到这一点,因为我想保持我如何实例化我的布局与该侦听器分开.
我已经尝试在重写方法内部的所有代码周围创建一个最初设置为false的if语句,但是无法知道何时将其设置为true,因为重写的方法在onCreate(),onStart()之后运行,并且每次onResume()方法.
我没有找到任何明确的答案.任何明确的解决方案将不胜感激.
我创建了一个微调器,当一个人使用阵列适配器添加设备时,该微调器会自动更新设备名称.我使用微调器创建了一个OnItemSelected方法,因此当选择微调器中的一个名称时,会出现一个新窗口.但是,OnItemSelected会在活动开始时自动选择列表中的第一个项目,因此用户在新窗口出现之前无法实际进行选择.
这是代码:
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
startActivity(new Intent("com.lukeorpin.theappliancekeeper.APPLIANCESELECTED"));
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Run Code Online (Sandbox Code Playgroud)
有没有人知道列表中的第一项不会自动选择的方式?
以下是其余微调器的代码:
ArrayAdapter<String> appliancenameadapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, ApplianceNames); //Sets up an array adapter containing the values of the ApplianceNames string array
applianceName = (Spinner) findViewById(R.id.spinner_name); //Gives the spinner in the xml layout a variable name
applianceName.setAdapter(appliancenameadapter); //Adds the contents of the array adapter into the spinner
applianceName.setOnItemSelectedListener(this);
Run Code Online (Sandbox Code Playgroud)