我是Android开发的新手。今天我尝试了一些我创建了一个名为 androidPickerviws 的项目。
在该项目中,我创建了两个活动,在Timepicker.java活动中,有一个名为 displayTime(hours,minute); 的方法。现在我怀疑是否可以在Datepicker.java活动中使用此方法displayTime()。如果是这样,我该怎么做..
这里我提供了我的Timepicker.java活动代码
package picker.view;
// imports...
public class Timepicker extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
LinearLayout linearlayout=new LinearLayout(this);
linearlayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutparams=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
LayoutParams params=new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
final TimePicker timepicker=new TimePicker(this);
linearlayout.addView(timepicker, params);
Button ok=new Button(this);
ok.setText("OK");
linearlayout.addView(ok, params);
addContentView(linearlayout,layoutparams);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
final int hours=timepicker.getCurrentHour();
final int minute=timepicker.getCurrentMinute();
displayTime(hours,minute);
}
private void displayTime(int hours, int minute) {
if(hours==0)
Toast.makeText(getBaseContext(), "Time saved is 12 : "+minute+" AM", Toast.LENGTH_SHORT).show();
else if(hours<12)
Toast.makeText(getBaseContext(), "Time saved is "+hours+" : "+minute+" AM", Toast.LENGTH_SHORT).show();
else if(hours==12)
Toast.makeText(getBaseContext(), "Time saved is "+hours+" : "+minute+" PM", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getBaseContext(), "Time saved is "+(hours-12)+" : "+minute+" PM", Toast.LENGTH_SHORT).show();
}
});
}
}
答案 0 :(得分:2)
我得到了我们可以这样调用的答案但是我们必须将该方法标记为公共,并且应该将所有方法的输出侧放在类中。修改后的代码如下
package picker.view;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TimePicker;
import android.widget.Button;
import android.widget.Toast;
public class Timepicker extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
/* setContentView(R.layout.timepicker);
final TimePicker timepicker=(TimePicker) findViewById(R.id.timePicker1);
Button ok=(Button) findViewById(R.id.button1);*/
LinearLayout linearlayout=new LinearLayout(this);
linearlayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutparams=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
LayoutParams params=new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
final TimePicker timepicker=new TimePicker(this);
linearlayout.addView(timepicker, params);
Button ok=new Button(this);
ok.setText("OK");
linearlayout.addView(ok, params);
addContentView(linearlayout,layoutparams);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
final int hours=timepicker.getCurrentHour();
final int minute=timepicker.getCurrentMinute();
displayTime(hours,minute);
}
});
}
public void displayTime(int hours, int minute) {
if(hours==0)
Toast.makeText(getBaseContext(), "Time saved is 12 : "+minute+" AM", Toast.LENGTH_SHORT).show();
else if(hours<12)
Toast.makeText(getBaseContext(), "Time saved is "+hours+" : "+minute+" AM", Toast.LENGTH_SHORT).show();
else if(hours==12)
Toast.makeText(getBaseContext(), "Time saved is "+hours+" : "+minute+" PM", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getBaseContext(), "Time saved is "+(hours-12)+" : "+minute+" PM", Toast.LENGTH_SHORT).show();
}
}
答案 1 :(得分:1)
您可以将代码放在“Utils”类中的静态方法中:
public class Utils {
private static void displayTime(Context context, int hours, int minute) {
if(hours==0) {
Toast.makeText(context, "Time saved is 12 : "+minute+" AM", Toast.LENGTH_SHORT).show();
}
else if(hours<12) {
Toast.makeText(context, "Time saved is "+hours+" : "+minute+" AM", Toast.LENGTH_SHORT).show();
}
else if(hours==12) {
Toast.makeText(context, "Time saved is "+hours+" : "+minute+" PM", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(context, "Time saved is "+(hours-12)+" : "+minute+" PM", Toast.LENGTH_SHORT).show();
}
}
}
然后在您的活动中使用以下方式调用它:
Utils.displayTime(this, 12, 45);