我有編輯文本框,會詢問用戶的日期。我想在編輯文本框中添加破折號,所以當用戶輸入日期時,它們不會消失。它應該是這樣的:如何在仍然存在的編輯文本中添加文本,即使用戶輸入了文本?
DD-MM-YYYY
因此,當用戶的破折號呆在那裏的時間進入數字。希望有人能爲我回答這個問題。我對Android非常陌生。
在此先感謝。
我有編輯文本框,會詢問用戶的日期。我想在編輯文本框中添加破折號,所以當用戶輸入日期時,它們不會消失。它應該是這樣的:如何在仍然存在的編輯文本中添加文本,即使用戶輸入了文本?
DD-MM-YYYY
因此,當用戶的破折號呆在那裏的時間進入數字。希望有人能爲我回答這個問題。我對Android非常陌生。
在此先感謝。
要回答你的問題,你需要在你EditText
視圖TextChangedListener
,您可以添加這樣的:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
在這三個方法,你可以處理保持短線位置的邏輯。
從用戶的角度(以及從開發人員的角度),這會很麻煩,更不用說它違背了design guidelines。你有兩個選擇。要麼你拆分editText
視圖分爲三個獨立的觀點它們之間呈現破折號與TextViews
,或者你使用的設計指南建議的解決方案中,DatePicker
它看起來像這樣:
當用戶選擇一個日期,可以編輯一個TextView
來顯示日期或任何你想要做的事情。我在截圖中使用的代碼如下:
活動
public class MainActivity extends ActionBarActivity {
private TextView textView;
private DatePicker datePicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.textView = (TextView) findViewById(R.id.textView1);
this.datePicker = (DatePicker) findViewById(R.id.datePicker1);
datePicker.init(2014, 4, 10, new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
textView.setText(view.getDayOfMonth() + "-" + view.getMonth() + "-" + view.getYear());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
佈局
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:spinnersShown="false" />
</RelativeLayout>
注意,我禁用了spinner
,因爲它需要大量的橫向空間。 如果必須保存視圖中的空間,也可以使用show the picker as a dialog。查看DatePicker的文檔以獲取更多信息。
你試過這種方法嗎?這應該將用戶的輸入與字符串或字符連接起來,而不是將其刪除。
editText.setText(editText.getText() + "stuff you want to add");
不過,說實話,在Android上,你應該使用可以使用這個DatePicker,絕對不是一個EditText。
我還沒有試過這個。現在就試試吧。謝謝。 – user3830113
首先,你應該需要使用日期選擇器拾取的日期,
如果是,你需要的話,那麼:
的EditText edit_datePicker =(EditText上)findViewById(R.id。 edit_pick); edit_datePicker.settext(「DD-MM-YYYY」); 現在,之後會出現什麼樣的用戶類型。
使用DatePicker的+1,我建議同樣的事情。自定義實現不僅更爲麻煩,而且顯然也與設計指南相沖突。 –
@VeselinRomic你是對的。我更新了我的答案以強調您的觀點。 – jhoanegar