1. android 开发环境搭建, 开发工具可以是eclipse+adt 或android studio。
2. 按照实验课要求制作对应的app小程序,实现按钮添加备忘,并且包含日期时间。
3. 编写并提交实验报告。
1.部分代码如下:
部分代码如下:
2、在MainActivity中显示布局及设定按钮功能
部分代码:
public class MainActivity extends AppCompatActivity {
public String makeTimeString(int year,int month,int day,int hour,int minute,String memoText) {
return year + "年" + (month + 1) + "月" + day + "日" + hour+"时" +minute+ "分------" + memoText;
}
public ArrayList list=new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button)findViewById(R.id.b1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
startActivityForResult(new Intent(MainActivity.this,Ctime.class),1);
}
});
}
protected void onActivityResult (int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode == 1){
int year =data.getIntExtra("year",0);
int month =data.getIntExtra("month",0);
int day = data.getIntExtra("day",0);
int hour = data.getIntExtra("hour",0);
int minute = data.getIntExtra("minute",0);
String memoText = data.getStringExtra("memoText");
String text = makeTimeString(year,month,day,hour,minute,memoText);
ArrayAdapter adapter =new ArrayAdapter(this,android.R.layout.simple_list_item_1,list);
adapter.add(text);
ListView listView =(ListView)findViewById(R.id.L1);
listView.setAdapter(adapter);
}
}
}
3、在Ctime中获取日期时间等功能
public class Ctime extends AppCompatActivity {
public int year;
public int month;
public int day;
public int hour;
public int minute;
public String memoText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ctime);
DatePicker dp=(DatePicker)findViewById(R.id.datePicker);
TimePicker tp=(TimePicker)findViewById(R.id.timePicker);
tp.setIs24HourView(true);
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
hour = c.get(Calendar.HOUR);
minute = c.get(Calendar.MINUTE);
dp.init(year, month, day, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker datePicker, int year, int month, int day) {
Ctime.this.year = year;
Ctime.this.month =month;
Ctime.this.day = day;
}
});
tp.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker timePicker, int hour, int minute) {
Ctime.this.hour = hour;
Ctime.this.minute =minute;
}
});
Button b2 =(Button)findViewById(R.id.bt1);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText text=(EditText)findViewById(R.id.edt1);
memoText =text.getText().toString();
Intent data =new Intent();
data.putExtra("year",year);
data.putExtra("month",month);
data.putExtra("day",day);
data.putExtra("hour",hour);
data.putExtra("minute",minute);
data.putExtra("memoText",memoText);
setResult(1,data);
finish();
}
});
}
public void Onclick(View view){
AlertDialog.Builder b = new AlertDialog.Builder(Ctime.this);
b.setTitle("请绘制:");
View MyView = new MyView(Ctime.this);
b.setView(MyView);
b.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
b.show();
}
}
4、新建MyView.java并完成绘制图像功能
public class MyView extends View {
float firstX;
float firstY;
Path path;
MyView(Context context)
{
super(context);
path = new Path();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStrokeWidth(10);
//让画出的图形是空心的
paint.setStyle(Paint.Style.STROKE);
canvas.drawPath(path,paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int act = event.getAction();
if (act == MotionEvent.ACTION_DOWN)
{
float x = event.getX();
float y = event.getY();
firstX = x;
firstY = y;
path.moveTo(firstX,firstY);
return true;
}
else if (act == MotionEvent.ACTION_MOVE)
{
float x = event.getX();
float y = event.getY();
path.lineTo(x,y);
invalidate();
}
return super.onTouchEvent(event);
}
}
当按动“添加图像”按钮按钮时,添加图像如图: