我在一个活动中包含 3 个按钮。第一个按钮用于启动带有 EditText 的自定义 AlertDialog。第二个按钮需要打开一个 DatePicker。第三个按钮需要打开一个 TimePicker。我的第一个按钮工作得很好,我有第二个按钮所需的代码,但我能让该代码工作的唯一方法是创建一个不同的活动。
我需要做什么才能在同一个 Activity 中实现所有这三件事?有没有可能,我对所有这些东西都很陌生,所以任何基本到高级的建议都会很棒!
这是带有三个按钮的活动的代码,所有这些按钮都在相应的 XML 文件中声明。这是我目前拥有的代码,这意味着只有 AlertDialog 的代码可以正常工作:
package com.example.test_project;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
public class NewWorkout extends Activity {
@Override
protected void onCreate(Bundle onSaveInstanceState) {
super.onCreate(onSaveInstanceState);
setContentView(R.layout.activity_new_workout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.new_workout, menu);
return true;
}
public void timeOfWorkout(View view){
Intent intent = new Intent(this, TimePickerFragment.class);
startActivity(intent);
}
int mYear;
int mMonth;
int mDay;
TextView mDateDisplay;
final int DATE_DIALOG_ID = 0;
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_workout);
mDateDisplay = (TextView) findViewById(R.id.timeOfWorkoutTextView);
Button button = (Button) findViewById(R.id.dateOfWorkoutButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// get the current date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// display the current date
updateDisplay();
}
});
DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
}
private void updateDisplay() {
this.mDateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));
}
public void nameOfWorkout(View view){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Enter a Name for This Workout");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
TextView edit = (TextView) findViewById(R.id.nameOfWorkoutTextView);
edit.setText(value);
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
}