如何使用日期选择器而不是在android中编辑文本框?

时间:2012-11-21 10:23:37

标签: android datepicker

现在我在android中使用一个Web服务。该Web服务有两个输入值 分别于01/01/2012和07/07/2012。

单击调用按钮并在将这些作为输入值提供后,我的项目将显示一些条形图。它的工作正常。这里我使用两个编辑文本框来获取这些输入值。

但我想使用Date-picker小部件而不是使用编辑文本框。所以我拖动了日期选择器小部件,我做了一些代码。

但现在的问题是,输入值是完全关闭数字和反斜杠对吗? 但是在日期选择器小部件中,它以字符和数字的组合显示输入值,但我只需要使用日期选择器小部件。

日期选择器小部件的格式为dd-mm-yyyy,例如: - 01-jan-1990

但我需要将上述格式转换为01/01/1990。怎么做?

请查找我的参考资料

Chart.java

public class Chart extends Activity 
{
//EditText edt1, edt2;

DatePicker dp1,dp2;

TextView txtv1;
Button btn;
Bundle data;

String[] orderNo = new String[20];
int[] freightRate = new int[20];
int[] marginPercent = new int[20];

@Override
public void onCreate(Bundle savedInstanceState)     {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    dp1 = (DatePicker) findViewById(R.id.datePicker_one);
    dp2 = (DatePicker) findViewById(R.id.datePicker_Two);

    String res_1 = dp1.getDayOfMonth()+"/"+dp1.getMonth()+"/"+dp1.getYear();
            String res_2 = dp2.getDayOfMonth()+"/"+dp2.getMonth()+"/"+dp2.getYear();   


            btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            getTMSChart(dp1.getYear(), dp2.getYear());
            Intent intnt = new Intent(v.getContext(), BarGraphActivity.class);
            intnt.putExtras(data);

            startActivity(intnt);
        }
    });
}

public void getTMSChart(int j, int k) 
{

    System.setProperty("http.keepAlive", "false");
    SoapSerializationEnvelope envelope = new   SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.dotNet = true;

    String METHOD = "GetTMSChart";
    String NAMESPACE = "http://tempuri.org/";
    String SOAP_ACTION = "http://tempuri.org/GetTMSChart";
    String URL = "http://54.251.60.177/TMSOrdersService/TMSDetails.asmx";

    SoapObject request = new SoapObject(NAMESPACE, METHOD);

    request.addProperty("FromDate", j);
    request.addProperty("ToDate", k);
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapObject result = (SoapObject) envelope.bodyIn;
        SoapObject root = (SoapObject) ((SoapObject) (result).getProperty(0)).getProperty("NewDataSet");
        int tablesCount = root.getPropertyCount();

        data = new Bundle();

    for (int i = 0; i < tablesCount; i++) {

    SoapObject table = (SoapObject) root.getProperty(i);
    orderNo[i] = table.getPropertyAsString("Order_No");
    freightRate[i] = Integer.parseInt(table.getPropertyAsString("Freight_Rate"));
    marginPercent[i] = Integer.parseInt(table.getPropertyAsString("Margin_Percent"));               

  /*Toast.makeText(getApplicationContext(), "Order No:" + orderNo[i] + "\nFreigtRate:" +     freightRate[i] + "\nMarginPc:" + marginPercent[i], Toast.LENGTH_SHORT).show();*/
        }
        data.putStringArray("orderno", orderNo);
        data.putIntArray("freightrate", freightRate);
        data.putIntArray("marginpercent", marginPercent);
        data.putInt("count",tablesCount);
    }
catch (Exception e) 
{
    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
    e.printStackTrace();
    }   } }

BarGraphActivity.java

public class BarGraphActivity extends Activity {

String[] orderNo = new String[20];
int[] freightRate = new int[20];
int[] marginPercent = new int[20];
int count;
RelativeLayout rltv;

@Override
    public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main1);

    final GraphicalView grfv = createIntent(); 

    RelativeLayout rltv = (RelativeLayout)findViewById(R.id.rlyot);
    rltv.addView(grfv);
}

public GraphicalView createIntent()
{
    rltv = (RelativeLayout)findViewById(R.id.rlyot);

    String[] titles = new String[] { "Order's profit of the year", " " };

    Bundle idata = getIntent().getExtras();
    if (idata !=null)
    {
        orderNo = idata.getStringArray("orderno");  
        freightRate = idata.getIntArray("freightrate");
        marginPercent = idata.getIntArray("marginpercent");
        count = idata.getInt("count");

        CategorySeries series = new CategorySeries("Order-FreightRate Graph");

        for ( int i = 0; i < count; i++)
        {
            series.add(orderNo[i], freightRate[i]);


            Thread th = new Thread();
            try 
            {
                Thread.sleep(2000);
                }
            catch (InterruptedException e) 
            {
                e.printStackTrace();
            }
        }


        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        dataset.addSeries(series.toXYSeries());

        XYSeriesRenderer renderer = new XYSeriesRenderer();
        renderer.setDisplayChartValues(true);

        //renderer.setChartValuesSpacing(1);

        XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
        mRenderer.addSeriesRenderer(renderer);

        mRenderer.setXLabelsColor(Color.WHITE);
        mRenderer.setYLabelsColor(0,Color.WHITE);

        mRenderer.setZoomButtonsVisible(true);

        mRenderer.setApplyBackgroundColor(true);
        mRenderer.setBackgroundColor(Color.parseColor("#000000"));

        mRenderer.setBarSpacing(0.7f);

    //  mRenderer.setChartTitleTextSize(20);
    //  mRenderer.setLabelsTextSize(25);
    //  mRenderer.setMarginsColor(Color.WHITE);

final GraphicalView grfv =  ChartFactory.getBarChartView(BarGraphActivity.this, dataset, mRenderer, Type.DEFAULT);
    return grfv;
    }
    return null;
    }
}

有什么想法吗?

感谢您宝贵的时间!..

1 个答案:

答案 0 :(得分:1)

这个怎么样:

保留文本字段,当选择textfeild时,会出现DateTime Picker对话框,textfield保留日期时间格式。

final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDisplay();

并在更新显示方法中:

private void updateDisplay() {
    checkdate.setText(
            new StringBuilder()
            // Month is 0 based so add 1
            .append(mMonth + 1).append("-")
            .append(mDay).append("-")
            .append(mYear).append(" "));
}

还要添加到活动

@Override
protected Dialog onCreateDialog(int id) {
    // TODO Auto-generated method stub
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this,
                mDateSetListener,
                mYear, mMonth, mDay);
 }
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    // TODO Auto-generated method stub
    switch (id) {
    case DATE_DIALOG_ID:
        ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
        break;
    }
}
private DatePickerDialog.OnDateSetListener mDateSetListener =
    new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        // TODO Auto-generated method stub
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        updateDisplay();
    }
};

将Focus监听器添加到文本字段

textfield1.setOnFocusChangeListener(new OnFocusChangeListener(){
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub
            if(hasFocus){
                showDialog(DATE_DIALOG_ID);
            }
        }
    });

希望你需要它。