2013-04-08 20 views
-3

我有一個編輯文本和一個文本視圖。 Textview將以字符串格式顯示日期。如果用戶在edittext中輸入10,則textview中的自動日期應該更新爲提前10天的顯示日期。如何添加在Android中的字符串格式的日期天數

如何做到這一點?

在此先感謝。

羅漢

+0

你不知道如何完成任務的一部分? – 2013-04-08 06:17:09

回答

0

首先你必須使用TextWatcher對你說,用戶的EditText和值,輸入值應在TextView的變化。和其他功能你沒有正確描述,但你可以使用Textwatcher做。

+0

假設用戶在edittext中輸入10,那麼必須在textview日期中添加10天,所有驗證和textview值應更改爲+10天。每月天n年驗證應該在那裏.. – Waugh 2013-04-08 06:34:04

8

請看到這一點,實現

public class MainActivity extends Activity { 

EditText edt_day; 
String day = ""; 
int next_date = 0; 
TextView txt_next_date; 

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

    edt_day = (EditText) findViewById(R.id.edt_day); 
    txt_next_date = (TextView) findViewById(R.id.txt_next_date); 

    edt_day.setOnEditorActionListener(new OnEditorActionListener() { 
     public boolean onEditorAction(TextView v, int actionId, 
       KeyEvent event) { 
      // Log.i("KeyBoard" ,"Inside the Edit Text"); 
      if (actionId == EditorInfo.IME_ACTION_GO) { 

       next_date = Integer.parseInt(edt_day.getText().toString()); 

       Calendar someDate = GregorianCalendar.getInstance(); 
       someDate.add(Calendar.DAY_OF_YEAR, +next_date); 
       SimpleDateFormat dateFormat = new SimpleDateFormat(
         "yyyy-MM-dd HH:mm:ss"); 
       String formattedDate = dateFormat.format(someDate.getTime()); 
       Log.e("minite", "formattedDate=" + formattedDate); 
       txt_next_date.setText(formattedDate); 
      } 
      return false; 
     } 
    }); 
    } 
} 




<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<EditText 
    android:id="@+id/edt_day" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:ellipsize="end" 
    android:hint="Enter no of Day" 
    android:imeOptions="actionGo" 
    android:singleLine="true" 
    android:textSize="14sp" /> 

<TextView 
    android:id="@+id/txt_next_date" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    tools:context=".MainActivity" /> 

</RelativeLayout> 
+0

請接受我的答案....... – somish 2013-05-02 09:58:24

相關問題