为什么我在DatePickerDialog中获得Extra Header?

时间:2016-02-24 08:54:34

标签: android

我正在获取DatePickerDialog的额外标题,如下面的快照所示,我不想这样,我在这里做错了什么?我需要没有那个标题的DatePickerDialog,请查看代码并让我知道哪个部分代码出错了。非常感谢您的帮助。

我的活动:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private CalendarView cal;
private EditText fromDateEtxt;
private DatePickerDialog fromDatePickerDialog;
private SimpleDateFormat dateFormatter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    fromDateEtxt = (EditText) findViewById(R.id.editDig);
    final InputMethodManager imm = (InputMethodManager)getSystemService(
            Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(fromDateEtxt.getWindowToken(), 0);
    cal = (CalendarView) findViewById(R.id.calendarView1);
    cal.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

        @Override
        public void onSelectedDayChange(CalendarView view, int year, int         month,
                                        int dayOfMonth) {
            // TODO Auto-generated method stub

            Toast.makeText(getBaseContext(), "Selected Date is\n\n"
                            + dayOfMonth + " : " + month + " : " + year,
                    Toast.LENGTH_LONG).show();
        }
    });

    dateFormatter = new SimpleDateFormat("dd-MM-yyyy");
    setDateTimeField();

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}



private void setDateTimeField() {
    fromDateEtxt.setOnClickListener(this);

    Calendar newCalendar = Calendar.getInstance();
    fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar newDate = Calendar.getInstance();
            newDate.set(year, monthOfYear, dayOfMonth);
            fromDateEtxt.setText(dateFormatter.format(newDate.getTime()));
        }


    }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

    Log.d("Date", "Current Date " + dateFormatter.format(new Date().getTime()) + "  Month  " + newCalendar.get(Calendar.MONTH));
   int maxYear =  newCalendar.get(Calendar.YEAR) - 18;

 /*   Calendar c2 = Calendar.getInstance();
    c2.set(Calendar.YEAR, maxYear);
    c2.set(Calendar.MONTH, newCalendar.get(Calendar.MONTH));
    c2.set(Calendar.DAY_OF_MONTH, newCalendar.get(Calendar.DAY_OF_MONTH));*/

    newCalendar.set(Calendar.YEAR, maxYear);
    Date d= new Date();
    d.setTime(newCalendar.getTimeInMillis());
    fromDatePickerDialog.getDatePicker().setMaxDate(d.getTime());
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View view) {
    if(view == fromDateEtxt) {
        fromDatePickerDialog.show();
    }

}
}

快照:

enter image description here

2 个答案:

答案 0 :(得分:3)

以下代码行设置对话框标题值:

fromDatePickerDialog.getDatePicker().setMaxDate(d.getTime());

为了清除这个额外的标题,您需要在此之后调用setTitle

fromDatePickerDialog.getDatePicker().setMaxDate(d.getTime());    
fromDatePickerDialog.setTitle(null);

因此,您将拥有最大日期对话框,并且不显示标题。

答案 1 :(得分:0)

使用以下任何一种方法,它会对你有所帮助。

1)尝试将标题Emopty设置为

     fromDatePickerDialog.setTitle("");

2)尝试不设置日期选择器的标题,如

 fromDatePickerDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);