我有一個Activity中包含3個按鈕。第一個按鈕用於使用EditText啓動自定義AlertDialog。第二個按鈕需要打開DatePicker。第三個按鈕需要打開一個TimePicker。我有第一個按鈕工作得很好,而且我有第二個按鈕需要的代碼,但唯一能讓代碼工作的方法是創建一個不同的活動。在同一個類/活動中使用TimePicker,DatePicker和AlertDialog
我需要做什麼才能在同一活動中實現所有這三件事?是否有可能,我對這些東西都很陌生,所以任何基本的高級建議都會很棒!
下面是三個按鈕的活動代碼,所有這些按鈕都在相應的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();
}
}
你能告訴我們你的相關問題代碼? – 2013-05-01 00:11:00
是的,添加活動的源代碼 – Booger 2013-05-01 00:12:05