2013-04-28 28 views
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開發或不同的活動和文件如何協同工作,所以任何幫助將是不勝感激!!!

回答

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

相關問題