2014-07-22 26 views

回答

2

要回答你的问题,你需要在你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它看起来像这样:

DatePicker screenshot
当用户选择一个日期,可以编辑一个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的文档以获取更多信息。

+1

使用DatePicker的+1,我建议同样的事情。自定义实现不仅更为麻烦,而且显然也与设计指南相冲突。 –

+1

@VeselinRomic你是对的。我更新了我的答案以强调您的观点。 – jhoanegar

0

你试过这种方法吗?这应该将用户的输入与字符串或字符连接起来,而不是将其删除。

editText.setText(editText.getText() + "stuff you want to add"); 

不过,说实话,在Android上,你应该使用可以使用这个DatePicker,绝对不是一个EditText。

+0

我还没有试过这个。现在就试试吧。谢谢。 – user3830113

0

首先,你应该需要使用日期选择器拾取的日期,

如果是,你需要的话,那么:

的EditText edit_datePicker =(EditText上)findViewById(R.id。 edit_pick); edit_datePicker.settext(“DD-MM-YYYY”); 现在,之后会出现什么样的用户类型。

相关问题