【问题标题】:TextView & Button Not Showing After Add DatePicker In Android在 Android 中添加 DatePicker 后,TextView 和按钮不显示
【发布时间】: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


    【解决方案1】:

    在最初将布局 R.layout.activity_user_dob 设置为内容视图后,您再次调用 setContentView(linearLayoutCalTvContainer);。这将覆盖先前设置的布局,并且该布局中包含的所有内容都将消失。为什么不只在 xml 中创建完整的布局?试试这样的:

    把你的 DatePicker 和 TextView 放到你想要的任何地方:

    <DatePicker
            android:id="@+id/datePicker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    <TextView 
            android:id="@+id/textViewUserDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    

    使用setContentView(R.layout.activity_user_dob); 设置布局后,您可以像这样检索DatePickerTextView 的实例:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_dob);
    
        // Get the instances of your DatePicker and TextView from the layout
        DatePicker datePickerBirthday = findViewById(R.id.datePicker);
        TextView textViewUserDate = findViewById(R.id.textViewUserDate);
    
        // Continue with the rest of the setup
        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= 11) {
            datePickerBirthday.setCalendarViewShown(false);
        }
        ...
    }
    

    但在您最初将布局设置为 R.layout.activity_user_dob 后,请务必不要再调用 setContentView(...);

    【讨论】:

    • 完成(y)。你的逻辑是正确的
    猜你喜欢
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多