首先,我是Android开发的新手,并且大多是全新的编程,所以请耐心等待。我有一个带有几个按钮的Activity。第一个按钮“锻炼名称”打开一个自定义AlertDialog,让我输入一些文本,然后将该文本放在按钮右侧的文本视图中。
我想要第二个按钮,“锻炼日期”,就是在同一活动中打开日期选择器对话框,方式与第一个按钮的工作方式相同。我最终拼凑了正确的代码以使其工作,但在一个不同的活动。所以我真的需要弄清楚如何修改这段代码:
package com.example.test_project;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class TimePickerFragment extends Activity {
/** Called when the activity is first created. */
private TextView mDateDisplay;
private Button mPickDate;
private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_workout);
// capture our View elements
mDateDisplay = (TextView) findViewById(R.id.timeOfWorkoutTextView);
mPickDate = (Button) findViewById(R.id.dateOfWorkoutButton);
// add a click listener to the button
mPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
// 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 (this method is below)
updateDisplay();
}
// updates the date in the TextView
private void updateDisplay() {
StringBuilder string1 = new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" ");
mDateDisplay.setText(string1);
}
// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
mDay);
}
return null;
}
}
在此代码的timeOfWorkout方法中工作:
package com.example.test_project;
import android.app.Activity;
import android.app.AlertDialog;
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.EditText;
import android.widget.TextView;
public class NewWorkout extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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;
}
TextView mDateDisplay;
Button mPickDate;
int mYear;
int mMonth;
int mDay;
final int DATE_DIALOG_ID = 0;
public void timeOfWorkout(View view){
}
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);
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
}
我已经尝试过几个小时修改它,并且总是遇到一个或另一个问题...我很确定我没有足够的Java或Android开发的基本掌握或不同的活动和文件一起工作,使这项工作,所以任何帮助都会得到很大的赞赏!!!
答案 0 :(得分:0)
在你的一个课程中,你打电话。
showDialog(DATE_DIALOG_ID);
在另一堂课
你打电话
alert.show()
调用.show()
可能会有问题(例如,当您的活动在后台时),最好拨打showDialog()
听起来你需要的是两个对话框ID。
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
mDay);
case ALERT_DIALOG_ID:
// DO YOUR BUILDER STUFF HERE, possibly offload to another method
// DO NOT call alert.show() instead
return alert.create()
}
return null;
}
}
现在,只要您想显示对话框调用showDialog(DATE_DIALOG_ID);
或showDialog(ALERT_DIALOG_ID);`
同时确保DATE_DIALOG_ID与ALERT_DIALOG_ID不同