public class PickerView extends FrameLayout implements OnClickListener{
DatePickerDialog.OnDateSetListener date;
Calendar myCalendar;
EditText pickerText;
String dateformat;
public PickerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.custom_keyboard);
dateformat = a.getString(R.styleable.custom_keyboard_mydateformat);
if (dateformat == null) {
dateformat="dd.MM.yyyy";
}
a.recycle();
if(!isInEditMode())
init(context);
Log.d("PickerView", dateformat);
// TODO Auto-generated constructor stub
}
public PickerView(Context context) {
super(context);
if(!isInEditMode())
init(context);
// TODO Auto-generated constructor stub
}
public PickerView(Context context, AttributeSet attrs) {
super(context);
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.custom_keyboard);
dateformat = a.getString(R.styleable.custom_keyboard_mydateformat);
if (dateformat == null) {
dateformat="dd.MM.yyyy";
}
a.recycle();
if(!isInEditMode())
init(context);
Log.d("PickerView", dateformat);
// TODO Auto-generated constructor stub
}
void init(Context context){
LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View picker=mInflater.inflate(R.layout.picker_layout, this, true);
myCalendar = Calendar.getInstance();
date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
Button button=(Button)picker.findViewById(R.id.editPicker1);
button.setOnClickListener(this);
pickerText=(EditText)picker.findViewById(R.id.picker1Text);
}
@Override
public void onClick(View v) {
if(pickerText.getText().length()==0)
{
new DatePickerDialog(getContext(), date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
else
{
try {
java.util.Date dateF=new SimpleDateFormat( dateformat )
.parse( pickerText.getText().toString() );
myCalendar.setTime(dateF);
new DatePickerDialog(getContext(), date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void updateLabel() {
SimpleDateFormat sdf = new SimpleDateFormat(dateformat, Locale.US);
pickerText.setText(sdf.format(myCalendar.getTime()));
}
public void setDateformat(String dateformat) {
//this.dateformat = dateformat;
//updateLabel();
}
我的活动
public class MainActivity extends Activity {
PickerView picker;
EditText editText;
Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
picker=(PickerView)findViewById(R.id.pickerView1);
editText=(EditText)findViewById(R.id.editeDateF);
bt=(Button)findViewById(R.id.buttonCl1);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
picker.setDateformat("dd/MM/yyyy");
//there i get error
}
});
}
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<com.gevorg89.PickerView.PickerView
android:id="@+id/pickerView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
custom:mydateformat="dd.MM.yyyy"
>
</com.gevorg89.PickerView.PickerView>
<EditText
android:id="@+id/editeDateF"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="dd|MM|yyyy"
android:inputType="date" >
</EditText>
<Button
android:id="@+id/buttonCl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
错误日志
12-15 19:38:50.923: E/AndroidRuntime(631): FATAL EXCEPTION: main
12-15 19:38:50.923: E/AndroidRuntime(631): java.lang.NullPointerException
12-15 19:38:50.923: E/AndroidRuntime(631): at com.example.customview.MainActivity$1.onClick(MainActivity.java:28)
12-15 19:38:50.923: E/AndroidRuntime(631): at android.view.View.performClick(View.java:3511)
12-15 19:38:50.923: E/AndroidRuntime(631): at android.view.View$PerformClick.run(View.java:14105)
12-15 19:38:50.923: E/AndroidRuntime(631): at android.os.Handler.handleCallback(Handler.java:605)
12-15 19:38:50.923: E/AndroidRuntime(631): at android.os.Handler.dispatchMessage(Handler.java:92)
12-15 19:38:50.923: E/AndroidRuntime(631): at android.os.Looper.loop(Looper.java:137)
12-15 19:38:50.923: E/AndroidRuntime(631): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-15 19:38:50.923: E/AndroidRuntime(631): at java.lang.reflect.Method.invokeNative(Native Method)
12-15 19:38:50.923: E/AndroidRuntime(631): at java.lang.reflect.Method.invoke(Method.java:511)
12-15 19:38:50.923: E/AndroidRuntime(631): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-15 19:38:50.923: E/AndroidRuntime(631): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-15 19:38:50.923: E/AndroidRuntime(631): at dalvik.system.NativeStart.main(Native Method)