问题描述
我想知道如何调用一个类的非静态方法的类。
I want to understand how to call a non-static method of one class in another class.
我已经看到类似以下内容:
I have seen something like the following:
Class1 c1 = new Class1();
c1.doSomething();
当Class1的方法中调用另一个类。
when the method Class1 is called in another class.
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}
我将不胜AP preciate详细的解释。是的,我已经研究这个概念,但我不能掌握它。谢谢!
I would greatly appreciate a detailed explanation. Yes, I have researched this notion, but I just cannot grasp it. Thanks!
推荐答案
您不需要类继承在所有acheive在你的问题的问题。
You don't need class inheritance at all to acheive the problem in your question.
我能一起抛出此工作的例子,既复位采摘为当前日期。
I was able to throw this working example together that resets both pickers to the current date.
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>
或者,如果你想方法来设置日期和时间,你可以做到这一点。
Or if you want methods to set the date and time, you can do this
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);
}
这篇关于如何调用另一个类的非静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!