【发布时间】:2014-03-23 13:58:14
【问题描述】:
我是 Android 新手。我有一个应用程序,它使用 DatePicker 选择用户的出生日期。当我添加此功能时,TextView & Button 等 XML 元素未显示。这怎么可能?我可以知道实现目标的正确方法是什么吗?也许这个问题太基础了,但我没有找到任何合适的解决方案。请帮帮我
这是我的代码:
public class UserDobActivity extends Activity {
DatePicker datePickerBirthday;
TextView textViewUserDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_dob);
ActionBar ab = getActionBar();
ab.setDisplayUseLogoEnabled(false);
ab.setDisplayShowHomeEnabled(true);
ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
ab.setCustomView(R.layout.actionbar);
ab.setDisplayHomeAsUpEnabled(true);
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#33CCFF"));
ab.setBackgroundDrawable(colorDrawable);
// create the date picker
datePickerBirthday = new DatePicker(this);
// hide the whole calendar view (works in api 11 or greater)
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 11) {
datePickerBirthday.setCalendarViewShown(false);
}
// create the TextView
textViewUserDate = new TextView(this);
textViewUserDate.setGravity(Gravity.CENTER);
// initialize the date to current date
SimpleDateFormat sdfDateTime = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
String dateStr = sdfDateTime.format(new Date(System.currentTimeMillis()));
String[] dateSplit = dateStr.split("-");
int currentYear = Integer.parseInt(dateSplit[0]);
int currentMonth = Integer.parseInt(dateSplit[1]);
int currentDay = Integer.parseInt(dateSplit[2]);
// to show date and day of week in the TextView
setHumanReadableDate(currentYear, currentMonth, currentDay);
// initialize date picker listener
// currentMonth - 1, because on the picker, 0 is January
datePickerBirthday.init(currentYear, currentMonth - 1, currentDay, birthdayListener);
// add to the container
LinearLayout linearLayoutCalTvContainer = new LinearLayout(this);
linearLayoutCalTvContainer.setOrientation(LinearLayout.VERTICAL);
linearLayoutCalTvContainer.addView(datePickerBirthday);
linearLayoutCalTvContainer.addView(textViewUserDate);
// set the views for the activity
setContentView(linearLayoutCalTvContainer);
RelativeLayout layoutCalTvContainer = (RelativeLayout) findViewById(R.id.layout);
layoutCalTvContainer.addView(datePickerBirthday);
}
// the date picker listener
OnDateChangedListener birthdayListener = new OnDateChangedListener() {
public void onDateChanged(DatePicker birthDayDatePicker,
int newYear, int newMonth, int newDay) {
try{
// currentMonth + 1, to retrieve proper month
setHumanReadableDate(newYear, newMonth + 1, newDay);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
};
// show the user a better date format
@SuppressLint("SimpleDateFormat") public void setHumanReadableDate(int newYear, int newMonth, int newDay){
try {
String clickedDate = newYear + "-" + newMonth + "-" + newDay;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = null;
try {
d = sdf.parse(clickedDate);
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleDateFormat sdfDateTime = new SimpleDateFormat("MMMM dd, yyyy 'is' EEEE", Locale.US);
String dateStr = sdfDateTime.format(d);
textViewUserDate.setText(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.user_dob, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, UserDetailsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onBackPressed() {
moveTaskToBack(true);
UserDobActivity.this.finish();
}}
这里是 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@android:color/white">
<TextView
android:id="@+id/text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/dobdetails"
android:textSize="30sp"
android:layout_marginTop="50dp"
android:gravity="center_horizontal" />
<RelativeLayout
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
</RelativeLayout>
<Button
android:id="@+id/ButtonUserDOB"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="@string/next"
android:background="@drawable/customised_button_click"
android:textSize="20sp"
/>
</LinearLayout>
【问题讨论】:
标签: android android-layout datepicker