2013-08-28 17 views
0

我正在開發一個Android應用程序,其中日期選擇器在那裏選擇出生日期...我需要在文本視圖中顯示此日期,並且必須添加此日期到一個單一的數字......並顯示在一個文本視圖..我沒有代碼在日期displyed在一個文本視圖,但我需要顯示它的總和..Android Datepicker日期添加和顯示在一個數字

例如,如果出生日期是04 02 1984然後0 + 4 0 + 2 1 + 9 + 8 + 4所以答案是4,2,4,並且這必須在文本視圖中單獨顯示。

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


    final DatePicker date = (DatePicker) findViewById(R.id.datePicker1); 
    final TextView tvDate = (TextView) findViewById(R.id.textView2); 


    date.init(date.getYear(), date.getMonth(), date.getDayOfMonth(),new OnDateChangedListener() 
    { 


    @Override 
    public void onDateChanged(DatePicker arg0, int arg1, int arg2, int arg3) { 
     // TODO Auto-generated method stub 
     int sum = 0; 
     String date=Integer.toString(arg3); 
     String month=Integer.toString(arg2); 
     String year=Integer.toString(arg1); 

     tvDate.setText(month+date+year); 

    } 


    } ); 
} 
} 
+0

的 「求和」 功能爲什麼'04 02 1984' 年給 '4,2,4'?它應該不是'4,2,2'嗎? – JavaDM

+0

no ....,1 + 9 = 10,8 + 4 = 12 so 12 + 10 = 22 and 2 + 2 = 4 – roshanpeter

回答

0

如果我理解你正確,那麼你在找什麼是鑄造的String您onDateChanged方法整數,所以可能b是這樣的:

int i = 0 ; 
while(i<2) // 2 for day and month 4 for year ... or what ever fits u can also run until the length of the string 
dateDigit += Integer.parseInt.subString(i++,i++); 
//this is just an idea , havent tested the code or anything 

繼續這樣做,直到你有一位數號碼(Link to post for this) 或MAYB一些遞歸方法,將繼續這樣做,直到你只有一個數字

希望這符合

1

CALL的每一天,月,年的變量

int daySum=sum(Integer.parseInt(day)); 
int monthSum=sum(Integer.parseInt(month)); 
int yearSum=sum(Integer.parseInt(year)); 
tvDay.setText(daySum); 
tvMonth.setText(monthSum); 
tvYear.setText(yearSum); 

int sum(int number){ 
     int remainder = number % 9; 
     if (number == 0) { 
      return number; 
     } else { 
      if (remainder == 0) { 
       return number; 
      }else{ 
       return remainder; 
      } 
     } 
} 
+0

roshanpeter檢查此功能它將滿足您的要求。 –