Answers:
来自http://developer.android.com/reference/android/app/Activity.html
public final void showDialog(int id)在API级别1中添加
此方法在API级别13中已弃用。请改为将新的DialogFragment类与FragmentManager一起使用;在旧版平台上,也可以通过Android兼容性软件包获得此功能。
不带任何参数的showDialog(int,Bundle)的简单版本。只需使用空参数调用showDialog(int,Bundle)。
为什么
怎么解决?
更多
不建议使用此方法。
使用新DialogFragment
类FragmentManager
代替;在旧版平台上,也可以通过Android兼容性软件包获得此功能。
要显示对话框,可以使用以下代码。这是为了显示一个带有多个复选框的简单AlertDialog框:
AlertDialog.Builder alertDialog= new AlertDialog.Builder(MainActivity.this); .
alertDialog.setTitle("this is a dialog box ");
alertDialog.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(),"ok ive wrote this 'ok' here" ,Toast.LENGTH_SHORT).show();
}
});
alertDialog.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "cancel ' comment same as ok'", Toast.LENGTH_SHORT).show();
}
});
alertDialog.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), items[which] +(isChecked?"clicked'again i've wrrten this click'":"unchecked"),Toast.LENGTH_SHORT).show();
}
});
alertDialog.show();
而如果您使用showDialog函数来显示不同的对话框或根据传递的参数来显示任何内容,则可以创建一个self函数,并可以在该onClickListener()
函数下调用它。就像是:
public CharSequence[] items={"google","Apple","Kaye"};
public boolean[] checkedItems=new boolean[items.length];
Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt=(Button) findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
display(0);
}
});
}
并在函数定义中添加上面给出的对话框代码。
这段代码对我有用。易于修复,但可能不是首选方法。
public void onClick (View v) {
createdDialog(0).show(); // Instead of showDialog(0);
}
protected Dialog createdDialog(int id) {
// Your code
}
package com.keshav.datePicker_With_Hide_Future_Past_Date;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
EditText ed_date;
int year;
int month;
int day;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed_date=(EditText) findViewById(R.id.et_date);
ed_date.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Calendar mcurrentDate=Calendar.getInstance();
year=mcurrentDate.get(Calendar.YEAR);
month=mcurrentDate.get(Calendar.MONTH);
day=mcurrentDate.get(Calendar.DAY_OF_MONTH);
final DatePickerDialog mDatePicker =new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday)
{
ed_date.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
int month_k=selectedmonth+1;
}
},year, month, day);
mDatePicker.setTitle("Please select date");
// TODO Hide Future Date Here
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
// TODO Hide Past Date Here
// mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis());
mDatePicker.show();
}
});
}
}
// Its Working