2014-07-22 28 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」); 現在,之後會出現什麼樣的用戶類型。

相關問題