package com.example.computer.myapplication;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
LinearLayout xingming;
EditText editText_xingming;
String string_xingming;
int m_year=2016,m_month=9,m_day=1;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(1,//组号
1,// 唯一ID
1,//排序号
"姓名" );
menu.add(1,2,2,"入学日期");
menu.add(1,3,3,"照片");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
switch (item.getItemId()) {
case 1:
xingming = (LinearLayout) getLayoutInflater().inflate(R.layout.xingming, null);
editText_xingming = (EditText) xingming.findViewById(R.id.editText_xingming);
dialog.setView(xingming);
dialog.setTitle("输入姓名");
dialog.setPositiveButton("确定", new okClick());
dialog.setNegativeButton("取消", new noClick());
dialog.create().show();
break;
case 2:
DatePickerDialog.OnDateSetListener dateSetListener=new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
m_year=year;
m_month=month;
m_day=dayOfMonth;
Toast.makeText(MainActivity.this,m_year+"年"+m_month+"月"+m_day+"日",Toast.LENGTH_SHORT).show();
}
};
//创建日期对话框对象
DatePickerDialog date= new DatePickerDialog(MainActivity.this,dateSetListener,m_year,m_month,m_day);
date.setTitle("日期对话框");
date.show();
break;
case 3:
Toast.makeText(this, string_xingming, Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
class okClick implements DialogInterface.OnClickListener{
@Override
public void onClick(DialogInterface dialog, int which) {
string_xingming=editText_xingming.getText().toString();
}
}
class noClick implements DialogInterface.OnClickListener{
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();//关闭对话框
}
}
}
// Intent intent = new Intent(MainActivity.this,Main2Activity.class);
// Bundle bundle = new Bundle();
// bundle.putString("txt",m_year+"年"+m_month+"月"+m_day+"日");
// intent.putExtras(bundle);
// startActivity(intent);
|