在同一活动中使用不同类型的对话
问题描述:
首先,我是全新的android开发人员,并且大多数人都是编程新手,所以请耐心等待。我有一个活动与几个按钮。第一个按钮“锻炼名称”打开了一个自定义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开发或不同的活动和文件如何协同工作,所以任何帮助将是不胜感激!!!
答
在你打电话的课程中。
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