-4

我有一个编辑文本和一个文本视图。Textview 将以字符串格式显示日期。如果用户在 edittext 中输入 10,则 textview 中的自动日期应更新为显示提前 10 天的日期。

这个怎么做?

提前致谢。

罗汉

4

2 回答 2

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>
于 2013-04-08T06:47:06.240 回答
0

First of all You have to use TextWatcher for as you say that user enter value in edittext and value should be change in Textview. and other functions you have not described properly,but you can do it using Textwatcher.

于 2013-04-08T06:24:26.180 回答