我想了解如何在另一个类中调用一个类的非静态方法。
我见过以下内容:
Class1 c1 = new Class1();
c1.doSomething();
在另一个类中调用方法Class1时。
public class AddA extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_a);
final DatePicker datePicker = (DatePicker) this.findViewById(R.id.DatePicker);
final TimePicker timePicker = (TimePicker) this.findViewById(R.id.TimePicker);
Button clear = (Button) this.findViewById(R.id.clear);
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
subject.setText("");
message.setText("");
//DatePicker.setCurrentDateOnView();
//TimePicker.setCurrentTimeOnView();
DateTimePicker datePicker = new DateTimePicker();
datePicker.setCurrentDateOnView(); //null here
}
});
//...More code
}
public class DateTimePicker extends AddA {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_alerts);
setCurrentDateOnView();
setCurrentTimeOnView();
}
// display current date
public void setCurrentDateOnView() { //null here
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into datepicker
dpResult.init(year, month, day, null);
}
///..More code}
我非常感谢详细解释。是的,我研究了这个概念,但我无法理解。谢谢!
答案 0 :(得分:1)
根本不需要类继承来解决问题中的问题。
我能够将这个工作示例放在一起,将两个选择器重置为当前日期。
<强> MainActivity 强>
public class MainActivity extends Activity {
private Button resetButton;
private DatePicker datePicker;
private TimePicker timePicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resetButton = (Button) findViewById(R.id.btn_reset);
datePicker = (DatePicker) findViewById(R.id.datePicker);
timePicker = (TimePicker) findViewById(R.id.timePicker);
resetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
datePicker.init(year, month, day, null);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
timePicker.setCurrentHour(hour);
timePicker.setCurrentMinute(minute);
}
});
}
}
<强> activity_main.xml中强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Reset"
android:id="@+id/btn_reset"/>
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/datePicker"
android:layout_gravity="center_horizontal"/>
<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="@+id/timePicker"/>
</LinearLayout>
或者,如果您想要方法来设置日期和时间,您可以这样做
resetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar c = Calendar.getInstance();
setDate(c);
setTime(c);
}
});
} // end onCreate
private void setDate(Calendar c) {
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
datePicker.init(year, month, day, null);
}
private void setTime(Calendar c) {
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
timePicker.setCurrentHour(hour);
timePicker.setCurrentMinute(minute);
}