0

首先,我是 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 开发或不同活动和文件的工作方式没有足够的基本了解一起完成这项工作,所以任何帮助将不胜感激!!!

4

1 回答 1

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 不同

于 2013-04-28T21:35:25.840 回答