简体   繁体   中英

import class/method from java file

I'm currently learning android and java, and I am having some issues with importing a java file.

I am working with the time and date example in the android api demos, and I want to put all the code relating to creating the time and date dialogs setting the values etc into a separate java file, and in my main java file run when the application starts, just declare the buttons and onclick events.

What is the best way to do this? Do I need to put all the code inside a class(that extends activity) or method? How do I use this inside my main class?

The code for the example is:

package com.datetest;
import android.app.Activity;
import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.view.View;
import java.util.Calendar;



public class datetest extends Activity {
   // where we display the selected date and time
private TextView mDateDisplay;

// date and time
private int mYear;
private int mMonth;
private int mDay;
private int mHour;
private int mMinute;

static final int TIME_DIALOG_ID = 0;
static final int DATE_DIALOG_ID = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    mDateDisplay = (TextView) findViewById(R.id.dateDisplay);

    Button pickDate = (Button) findViewById(R.id.pickDate);
    pickDate.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });

    Button pickTime = (Button) findViewById(R.id.pickTime);
    pickTime.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            showDialog(TIME_DIALOG_ID);
        }
    });

    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);
    mHour = c.get(Calendar.HOUR_OF_DAY);
    mMinute = c.get(Calendar.MINUTE);

    updateDisplay();
}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case TIME_DIALOG_ID:
            return new TimePickerDialog(this,
                    mTimeSetListener, mHour, mMinute, false);
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this,
                        mDateSetListener,
                        mYear, mMonth, mDay);
    }
    return null;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    switch (id) {
        case TIME_DIALOG_ID:
            ((TimePickerDialog) dialog).updateTime(mHour, mMinute);
            break;
        case DATE_DIALOG_ID:
            ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
            break;
    }
}    

private void updateDisplay() {
    mDateDisplay.setText(
        new StringBuilder()
                // Month is 0 based so add 1
                .append(mMonth + 1).append("-")
                .append(mDay).append("-")
                .append(mYear).append(" ")
                .append(pad(mHour)).append(":")
                .append(pad(mMinute)));
}

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();
            }
        };

private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {

            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mHour = hourOfDay;
                mMinute = minute;
                updateDisplay();
            }
        };

private static String pad(int c) {
    if (c >= 10)
        return String.valueOf(c);
    else
        return "0" + String.valueOf(c);
}

}

Thanks for your help

Why do you need to separate this?

imho you need a different approach if you want the kind of separation you are describing. The methods and classes in the code are used very tightly with cross references and such - It would be a mess to try and separate - so why bother?

Personally I'd go about the problem solved in this code a bit differently - ending with several classes pretty much as you described - but it would be built from the bottom up, not just from exsisting code designed to be tightly coupled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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