1,把showDatePickerDialog方法的入参中的final关键字去掉,设置成全局变量。
2,你的EditText.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
showDatePickerDialog(你的EditText);
}
return true;
}
});

解决方案 »

  1.   


    还是不行的,我把全部的代码贴出来能帮我看下码?
    public class DetailInfoViewActivity extends Activity implements OnClickListener {
    private Button previou;
    private Button next;
    private Button save;
    private Button back;
    private String fileAdr;
    private DatePickerDialog dpd=null;
    private final EditText[] et = new EditText[39]; private static final String TAG = " com.cz.file.app.DetailInfoViewActivity"; @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detailinfo_view); previou = (Button) this.findViewById(R.id.previou_button);
    next = (Button) this.findViewById(R.id.next_button);
    save = (Button) this.findViewById(R.id.detailinfo_save);
    back = (Button) this.findViewById(R.id.detailinfo_back);
    et[0] = (EditText) this.findViewById(R.id.DETAIL_VALUE1);
    et[1] = (EditText) this.findViewById(R.id.DETAIL_VALUE2);
    et[2] = (EditText) this.findViewById(R.id.DETAIL_VALUE3);
    et[3] = (EditText) this.findViewById(R.id.DETAIL_VALUE4);
    et[4] = (EditText) this.findViewById(R.id.DETAIL_VALUE5);
    et[5] = (EditText) this.findViewById(R.id.DETAIL_VALUE6);
    et[6] = (EditText) this.findViewById(R.id.DETAIL_VALUE7);
    et[7] = (EditText) this.findViewById(R.id.DETAIL_VALUE8);
    et[8] = (EditText) this.findViewById(R.id.DETAIL_VALUE9);
    et[9] = (EditText) this.findViewById(R.id.DETAIL_VALUE10);
    et[10] = (EditText) this.findViewById(R.id.DETAIL_VALUE11);
    et[11] = (EditText) this.findViewById(R.id.DETAIL_VALUE12);
    et[12] = (EditText) this.findViewById(R.id.DETAIL_VALUE13);
    et[13] = (EditText) this.findViewById(R.id.DETAIL_VALUE14);
    et[14] = (EditText) this.findViewById(R.id.DETAIL_VALUE15);
    et[15] = (EditText) this.findViewById(R.id.DETAIL_VALUE16);
    et[16] = (EditText) this.findViewById(R.id.DETAIL_VALUE17);
    et[17] = (EditText) this.findViewById(R.id.DETAIL_VALUE18);
    et[18] = (EditText) this.findViewById(R.id.DETAIL_VALUE19);
    et[19] = (EditText) this.findViewById(R.id.DETAIL_VALUE20);
    et[20] = (EditText) this.findViewById(R.id.DETAIL_VALUE21);
    et[21] = (EditText) this.findViewById(R.id.DETAIL_VALUE22);
    et[22] = (EditText) this.findViewById(R.id.DETAIL_VALUE23);
    et[23] = (EditText) this.findViewById(R.id.DETAIL_VALUE24);
    et[24] = (EditText) this.findViewById(R.id.DETAIL_VALUE25);
    et[25] = (EditText) this.findViewById(R.id.DETAIL_VALUE26);
    et[26] = (EditText) this.findViewById(R.id.DETAIL_VALUE27);
    et[27] = (EditText) this.findViewById(R.id.DETAIL_VALUE28);
    et[28] = (EditText) this.findViewById(R.id.DETAIL_VALUE29);
    et[29] = (EditText) this.findViewById(R.id.DETAIL_VALUE30);
    et[30] = (EditText) this.findViewById(R.id.DETAIL_VALUE31);
    et[31] = (EditText) this.findViewById(R.id.DETAIL_VALUE32);
    et[32] = (EditText) this.findViewById(R.id.DETAIL_VALUE33);
    et[33] = (EditText) this.findViewById(R.id.DETAIL_VALUE34);
    et[34] = (EditText) this.findViewById(R.id.DETAIL_VALUE35);
    et[35] = (EditText) this.findViewById(R.id.DETAIL_VALUE36);
    et[36] = (EditText) this.findViewById(R.id.DETAIL_VALUE37);
    et[37] = (EditText) this.findViewById(R.id.DETAIL_VALUE38);
    et[38] = (EditText) this.findViewById(R.id.DETAIL_VALUE39);
    fileAdr = (String) MyApplication.getInstance().getParameter("fileAdr");
    Integer index = (Integer) MyApplication.getInstance().getParameter(
    "nowItem");
    @SuppressWarnings("unchecked")
    List<MyItem> items = (List<MyItem>) MyApplication.getInstance()
    .getParameter("fileData");
    showData(index, items);
    save.setOnClickListener(this);
    previou.setOnClickListener(this);
    next.setOnClickListener(this);
    back.setOnClickListener(this); et[4].setOnClickListener(this);
    et[9].setOnClickListener(this);
    } @Override
    public void onClick(View arg0) {
    int id = arg0.getId();
    switch (id) {
    case R.id.previou_button:
    changeData(0);
    break;
    case R.id.next_button:
    changeData(1);
    break;
    case R.id.detailinfo_save:
    save();
    break;
    case R.id.detailinfo_back:
    this.finish();
    break;
    case R.id.DETAIL_VALUE5:
    break;
    case R.id.DETAIL_VALUE10:
    et[9].setOnTouchListener(new OnTouchListener(){ @Override
    public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        showDatePickerDialog(10);
                    }
    return true;
    }
    });
    break;
    case R.id.DETAIL_VALUE12://这个EditText不会弹出DatePickerDialog
    et[11].setOnTouchListener(new OnTouchListener(){ @Override
    public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        showDatePickerDialog(12);
                    }
    return true;
    }
    });
    break;
    case R.id.DETAIL_VALUE13://这个EditText不会弹出DatePickerDialog
    et[12].setOnTouchListener(new OnTouchListener(){ @Override
    public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        showDatePickerDialog(13);
                    }
    return true;
    }
    });
    break;
    default:
    break;
    }
    } private void showDatePickerDialog(final int index ) {
    Date date = new Date();
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    final int year = c.get(Calendar.YEAR);
    final int month = c.get(Calendar.MONTH);
    final int day = c.get(Calendar.DAY_OF_MONTH);
      dpd = new DatePickerDialog(this,
    new OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year,
    int monthOfYear, int dayOfMonth) {
    et[index-1].setText(year + "-" + monthOfYear + "-" + dayOfMonth);
    }
    }, year, month, day);
    dpd.setTitle("选择日期");
    dpd.show();
    } private void save() { String[] datas = new String[39];
    for (int i = 0; i < et.length; i++) {
    datas[i] = et[i].getText().toString();
    }
    Integer index = (Integer) MyApplication.getInstance().getParameter(
    "nowItem");
    @SuppressWarnings("unchecked")
    List<MyItem> items = (List<MyItem>) MyApplication.getInstance()
    .getParameter("fileData"); int EVT_UDFCHAR11 = convertInt(datas[29]);
    int EVT_UDFCHAR10 = convertInt(datas[26]);
    int EVT_UDFCHAR13 = convertInt(datas[27]);
    int EVT_UDFCHAR16 = convertInt(datas[28]);
    int EVT_UDFCHAR14 = convertInt(datas[30]);
    int EVT_UDFCHAR17 = convertInt(datas[31]);
    int EVT_UDFCHAR22 = convertInt(datas[32]);
    /**
     * EVT_UDFCHAR12=(EVT_UDFCHAR11-EVT_UDFCHAR10)/EVT_UDFCHAR22×100% 相对误差1
     * EVT_UDFCHAR15=(EVT_UDFCHAR14-EVT_UDFCHAR13)/EVT_UDFCHAR22×100% 相对误差2
     * EVT_UDFCHAR18=(EVT_UDFCHAR17-EVT_UDFCHAR16)/EVT_UDFCHAR22×100% 相对误差3
     */
    // 计算误差
    int v1 = (EVT_UDFCHAR22 != 0) ? ((EVT_UDFCHAR11 - EVT_UDFCHAR10) * 100 / EVT_UDFCHAR22)
    : 0;
    int v2 = (EVT_UDFCHAR22 != 0) ? ((EVT_UDFCHAR14 - EVT_UDFCHAR13) * 100 / EVT_UDFCHAR22)
    : 0;
    int v3 = (EVT_UDFCHAR22 != 0) ? ((EVT_UDFCHAR17 - EVT_UDFCHAR16) * 100 / EVT_UDFCHAR22)
    : 0;
    datas[33] = "" + v1;
    datas[34] = "" + v2;
    datas[35] = "" + v3;
    et[33].setText("" + v1);
    et[34].setText("" + v2);
    et[35].setText("" + v3);
    items.get(index).setValues(datas);
    MyApplication.getInstance().setParameter("fileData", items);// 更新缓存
    try {
    JXLExcelHandler.updateRow(fileAdr, index, datas);// 更新文件
    } catch (Exception e) {
    e.printStackTrace();
    }
    } private int convertInt(String value) {
    int val = 0;
    try {
    val = Integer.valueOf(value);
    } catch (Exception e) {
    val = 0;
    }
    return val;
    } /**
     * 
     * @Description: 上一条和下一条点击事件的通用方法
     * 
     * @param tag
     *            0表示上一条 1表示下一条
     */
    private void changeData(int tag) {
    Integer index = (Integer) MyApplication.getInstance().getParameter(
    "nowItem");
    @SuppressWarnings("unchecked")
    List<MyItem> items = (List<MyItem>) MyApplication.getInstance()
    .getParameter("fileData");
    switch (tag) {
    case 0:// 上一条
    if (index == 0) {// 如果已经是第一条记录
    break;
    }
    index--;
    break;
    case 1:// 下一条
    if (index == items.size() - 1) {
    break;
    }
    index++;
    break;
    default:
    break;
    }
    Log.i(TAG, "选择的第" + index + "条记录。");
    showData(index, items);
    } private void showData(int index, List<MyItem> items) {
    MyApplication.getInstance().setParameter("nowItem", index);// 改变当前条目索引
    MyItem item = items.get(index);
    for (int i = 0; i < et.length; i++) {
    et[i].setText(item.getValues()[i]);
    }
    }
    }有两个地方标注了//这个EditText不会弹出DatePickerDialog这个的不会弹出,第一个会弹出来的。什么情况这是。
      

  2.   

    你在 OnClick方法中才设置 OnTouchListener。所以肯定接收不到。
    试试在OnCreate里就设置OnTouchListener。
      

  3.   

    还有,你就 
    et[4].setOnClickListener(this);
    et[9].setOnClickListener(this);其他的et没有注册OnClickListener方法。
      

  4.   

    可以了现在,谢谢你,我现在还有一个问题:
       
    上面这张图片中,我想在public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button=(Button)this.findViewById(R.id.button);
    View v=this.findViewById(R.layout.dialog);//获取dialog视图,为什么老是获取为null
    System.out.println(v);
    button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    Dialog dialog=new Dialog(MainActivity.this);
    dialog.setTitle("XXX");
    dialog.setContentView(R.layout.dialog);
    dialog.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.main, menu);
    return true;
    }}
    我在MainActivity中获取dialog.xml文件视图的时候老是获取不到者是为什么?
      

  5.   

    View v=this.findViewById(R.layout.dialog);//获取dialog视图,为什么老是获取为null
    改成:
    View v = getLayoutInflater().inflate(R.layout.dialog, null);