我正在制作一个Android时间表应用程序,我有两个微调器,允许用户选择开始时间和讲座的持续时间。
没有讲座可以在晚上6点之后完成,但持续时间微调器允许讲座持续长达3个小时。我想要做的是,当用户选择4PM或5PM时,对于开始时间,持续时间的选项将根据此选择进行限制。例如,如果选择的开始时间是下午5点,则持续时间不能超过1小时。
谢谢! 源代码来自MainActivity.Java
public class MainActivity extends Activity
{
//Data types, inputs and buttons
private EditText codeInput;
private Spinner dayInput;
private Spinner timeInput;
private Spinner durationInput;
private Spinner sessionInput;
private EditText roomInput;
private Button addNewButton;
private Button displayTimetableButton;
//Creates the database handler
DatabaseHandler DBHandler;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Find spinners and EditText elements in layout
codeInput = (EditText) findViewById(R.id.CodeInput);
dayInput = (Spinner) findViewById(R.id.DayInput);
timeInput = (Spinner) findViewById(R.id.TimeInput);
durationInput = (Spinner) findViewById(R.id.DurationInput);
sessionInput = (Spinner) findViewById(R.id.SessionInput);
roomInput = (EditText) findViewById(R.id.RoomInput);
//Find buttons in layout
addNewButton = (Button) findViewById(R.id.AddNewButton);
displayTimetableButton = (Button) findViewById(R.id.DisplayTimetableButton);
addNewButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//this method collects and adds all of the input data to the database
String code = codeInput.getText().toString();
String day = dayInput.getSelectedItem().toString();
String time = timeInput.getSelectedItem().toString();
String duration = durationInput.getSelectedItem().toString();
String session = sessionInput.getSelectedItem().toString();
String room = roomInput.getText().toString();
DBHandler = new DatabaseHandler (getBaseContext());
DBHandler.open();
long DatabaseHandler = DBHandler.InsertData(code, day, time, duration, session, room);
//This toast appears when the module information is successfully added
Toast.makeText(getBaseContext(), "Module Added", Toast.LENGTH_LONG).show();
//These set the EditText fields to blank, ready for the next input
codeInput . setText("");
roomInput . setText("");
//Close the database connection
DBHandler.close();
}
});
displayTimetableButton.setOnClickListener(new View.OnClickListener()
{
//This method starts the GridView activity when the button is pressed
@Override
public void onClick(View v)
{
Intent i = new Intent(MainActivity.this, GridViewActivity.class);
startActivity(i);
}
});
}
}
选择
<string-array name="timeArray">
<item> 9:00AM </item>
<item> 10:00AM </item>
<item> 11:00AM </item>
<item> 12:00AM </item>
<item> 1:00PM </item>
<item> 2:00PM </item>
<item> 3:00PM </item>
<item> 4:00PM </item>
<item> 5:00PM </item>
</string-array>
<string-array name="durationArray">
<item>1 Hour </item>
<item>2 Hours </item>
<item>3 Hours </item>
</string-array>
答案 0 :(得分:0)
您可以跟踪所选项目并使用事件setOnItemSelectedListener
。要相应地更改第二个Spinner
项目。