2016-05-06 23 views
0

我正在處理一個包含查找欄的片段,如果使用了editText元素,則查找欄值會更新。用戶也可以通過在editText中輸入值來更新seekbar(這一切都有效,直到我實現清除先前的值)上一個EditText值按要求清除,但不允許輸入新用戶

seekbar默認爲500(中點),textView顯示500。 seekbar進度更改時,editText中的值與它匹配。 (這種作品)

當用戶通過選擇EditText元素輸入新值時,我希望500(或什麼值)消失爲新的輸入。 (這工作)。

問題是:每當我在軟鍵盤上輸入一個新值並選擇該值時,新值剛剛清除,softkeyboard仍然保持不變,並且進度條或edittext不更新。什麼做錯了?

搜索欄 - 更新的EditText

private int radius = 500; 
//START SEEKBAR DISTANCE 
distControl=(SeekBar)view.findViewById(R.id.sb_loc_dist_set); 
txtInMaxMiles=(EditText)view.findViewById(R.id.txt_max_num_input); 
distControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() 

{ 
    int progressChange = 0; 

    @Override 
    public void onProgressChanged (SeekBar seekBar,int progress, boolean fromUser){ 
    radius = progress; 
    txtInMaxMiles.setText(String.valueOf(radius)); 
} 
    @Override 
    public void onStartTrackingTouch (SeekBar seekBar){ 
    distControl.setProgress(radius); 
} 
    @Override 
    public void onStopTrackingTouch (SeekBar seekBar){ 
    txtInMaxMiles.setText(String.valueOf(radius)); 
}});//END: SEEK BAR DISTANCE 

編輯文本 - 更新進度條

txtInMaxMiles.addTextChangedListener(new TextWatcher() { 
     String txtIn; 

     @Override //Before screen fully loads 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
      // txtIn = txtInMaxMiles.getText().toString(); 
     } 
     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      txtIn = txtInMaxMiles.getText().toString(); 
     } 
     @Override 
     public void afterTextChanged(Editable s) { 

      txtIn = txtInMaxMiles.getText().toString(); 
      if (txtIn == null || txtIn.trim().equals("")) { 
       radius = 0; 
      } 
      if (radius < 1 || radius > 1000) { 
       txtInMaxMiles.setError("Please input a number between 0 and 1001"); 
      } else { 
       txtInMaxMiles.setError(null); 
       distControl.setProgress(Integer.parseInt(s.toString())); 
       // txtInMaxMiles.setText(Integer.toString(radius)); 
      } 
     } 
    }); 

Click上的EditText元素

txtInMaxMiles.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       txtInMaxMiles.setText(""); 
      } 
     }); 
     return view; 

我認爲這是一個quickfix,但即時失蹤,所以任何幫助將不勝感激。

回答

0

好吧,管理它的工作。這次我沒有硬編碼的半徑(淘氣),使代碼更清潔。但即使這樣做後...... OnClickStill沒有工作。所以我嘗試了另一種方法,其中文本被突出顯示,並在用戶輸入新值時替換,這個線程很有用:Select all text inside EditText when it gets focus 但是,我仍嘗試onClick ...並且它沒有工作,即使它離我們更近了一步。 最後Lukas Batteau在此線程上保存了一天Android EditText onClickListener 而不是使用onClick我更改爲OnTouchListener。

txtInMaxMiles.setSelectAllOnFocus(true); 
     txtInMaxMiles.addTextChangedListener(new TextWatcher() { 
      int txtIn; 

      @Override //Before screen fully loads 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
       txtInMaxMiles.clearFocus(); 
       txtInMaxMiles.requestFocus(); 
      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 

       txtIn = Integer.parseInt(txtInMaxMiles.getText().toString()); 

       if (txtInMaxMiles.getText().toString() == null || txtInMaxMiles.getText().toString().trim().equals("")) { 
       } 
       if (txtIn < 1 || txtIn > 1000) { 
        txtInMaxMiles.setError("Please input a number between 0 and 1001"); 
       } else { 

        txtInMaxMiles.setError(null); 
        distControl.setProgress(Integer.parseInt(s.toString())); 
       } 
      } 
     }); 

     txtInMaxMiles.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       if (MotionEvent.ACTION_UP == event.getAction()) { 
        txtInMaxMiles.clearFocus(); 
        txtInMaxMiles.requestFocus(); 
       } 
       return false; // return is important... 
      } 
     }); 
     return view; 
    } 

好了,我還是很是新手,所以任何清理編碼意見可以理解,如果有人知道原因onClickListener沒怎麼工作,請讓我知道,因爲我不知道爲什麼。

相關問題