我有编辑文本框,会询问用户的日期。我想在编辑文本框中添加破折号,所以当用户输入日期时,它们不会消失。它应该是这样的:如何在仍然存在的编辑文本中添加文本,即使用户输入了文本?
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