2 kevinjqy kevinjqy 于 2014.11.09 23:45 提问

fragment中额外加入时间选择器dialog的布局

public class ReleaseFragment extends Fragment implements View.OnTouchListener{

private EditText etStartTime; 

public ReleaseFragment() {
    // TODO Auto-generated constructor stub
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    etStartTime = (EditText)getActivity().findViewById(R.id.time_actionbar_release); 
    etStartTime.setOnTouchListener(this); 
    return inflater.inflate(R.layout.fragment_actionbar_release, null);

}

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    if (event.getAction() == MotionEvent.ACTION_DOWN) { 

        AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity()); 
        View view = View.inflate(this, R.layout.date_time_dialog, null); 
        final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker); 
        //以上两行报错,原因无法初始化两个布局
        builder.setView(view); 

        Calendar cal = Calendar.getInstance(); 
        cal.setTimeInMillis(System.currentTimeMillis()); 
        datePicker.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), null);  

        if (v.getId() == R.id.time_actionbar_release) { 
            final int inType = etStartTime.getInputType(); 
            etStartTime.setInputType(InputType.TYPE_NULL); 
            etStartTime.onTouchEvent(event); 
            etStartTime.setInputType(inType); 
            etStartTime.setSelection(etStartTime.getText().length()); 

            builder.setTitle("选取起始时间"); 
            builder.setPositiveButton("确  定", new DialogInterface.OnClickListener() { 

                @Override 
                public void onClick(DialogInterface dialog, int which) { 

                    StringBuffer sb = new StringBuffer(); 
                    sb.append(String.format("%d-%02d-%02d",  
                            datePicker.getYear(),  
                            datePicker.getMonth() + 1, 
                            datePicker.getDayOfMonth())); 
                    sb.append("  "); 

                    etStartTime.setText(sb);                      

                    dialog.cancel(); 
                } 
            }); 

        } 

        Dialog dialog = builder.create(); 
        dialog.show(); 
    }
    return true;
}

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!