繁体   English   中英

在同一活动中使用不同类型的对话框

[英]Using Different types of Dialogs in Same Activity

首先,我是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 Development或其他活动和文件的工作方式没有足够的基本了解一起使这项工作尚未完成,因此任何帮助将不胜感激!!!

在您的一门课中,您打电话。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM