2012-11-21 194 views
0

現在我正在使用android中的一個web服務。該web服務分別具有兩個輸入值 ,即01/01/2012和07/07/2012。如何在android中使用日期選擇器而不是編輯文本框?

單擊調用按鈕後,提供這些作爲輸入值後,我的項目會顯示一些條形圖。讓其工作正常。在這裏我使用兩個編輯文本框來獲取這些輸入值。

但我想使用日期選擇器小部件,而不是使用編輯文本框。所以我拖動了日期選擇器小部件,我做了一些代碼。

但現在問題是,輸入值完全關閉數字和反斜槓? 但在日期選擇器小部件中,它顯示字符和數字組合中的輸入值,但我只需要使用日期選擇器小部件。

日期選擇器窗口小部件是在這種格式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; 
    } 
} 

任何想法?

感謝您的寶貴時間..

+0

請解釋你的問題,並分享一些代碼 –

+0

@Sas請再次訪問我的問題,我已經更新我的問題與來源 – prabu

回答

1

這個怎麼樣!

你保持文本字段和選擇textfeild時,會出現一個DateTime選擇對話框,並在文本框保留日期時間格式。

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(); 
    } 
}; 

和焦點監聽器添加到文本框

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

希望其你需要什麼。

相關問題