2013-08-02 53 views
5

我想EditText暫時不會獲得FOUCS。臨時禁用的EditText

的情況是:我有兩個EditTexts。每當EditText的文本發生更改時,我都希望另一個EditText不會響應用戶單擊,直到發生另一個預定義的事件。

爲了實現這個目標我想這:

@Override 
protected void onResume() { 
    super.onResume(); 
    Log.d(TAG, "onResume"); 
    registerBroadcastReceivers(); 
} 

// Register BroadcastReceivers 
private void registerBroadcastReceivers() { 
    Log.d(TAG, "Registering broadcast receivers."); 

    // Google response receiver 
    googleResponseReceiver = new GoogleAPIResponseReceiver(); 
    IntentFilter googleResponseIntentFilter = new IntentFilter(
      RS_GoogleApiIntentService.ACTION_RECEIVED_GOOGLE_RESPONSE); 
    googleResponseIntentFilter.addCategory(Intent.CATEGORY_DEFAULT); 
    LocalBroadcastManager.getInstance(this).registerReceiver(
      googleResponseReceiver, googleResponseIntentFilter); 
} 

editText1.addTextChangedListener(new TextWatcher() { 
    @Override 
    public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 
     // Set EditText non-focusable 
     MyActivity.this.editText2.setFocusableInTouchMode(false); 
    } 
}); 

. 
. 
. 
// Somewhere in one function I call below function to start an IntentService. 
. 
. 
. 

// Call Google API with given URL 
private void CallGoogleApiWithUrl(String url, int requestId) { 

    Intent intent = new Intent(this, RS_GoogleApiIntentService.class); 
    intent.putExtra(RS_GoogleApiIntentService.EXTRA_URL_STRING, url); 
    intent.putExtra(RS_GoogleApiIntentService.EXTRA_GOOGLE_API_REQUEST_ID, 
      requestId); 
    startService(intent); 
} 

// Broadcast receiver for Google API response 
public class GoogleAPIResponseReceiver extends BroadcastReceiver { 

    // Enabling EditText works up to this point. 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     // Stops working now onwards. 

    } 
} 

listView.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 

     // Set EditText focusable 
     MyActivity.this.editText2.setFocusableInTouchMode(true); 
} 

佈局:

<LinearLayout 
    style="@style/create_trip_activity_components_style" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="30dp" 
    android:gravity="center_vertical" 
    android:orientation="horizontal" > 

    <EditText 
     android:id="@+id/from_location" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="5dp" 
     android:ems="10" 
     android:hint="@string/from_location_hint" 
     android:imeOptions="actionDone" 
     android:imeActionLabel="Done" 
     android:inputType="text" > 

     <requestFocus /> 
    </EditText> 

    <Button 
     android:id="@+id/use_current_button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:onClick="useCurrentLocation" 
     android:text="@string/use_current" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 
</LinearLayout> 

<ImageButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="20dp" 
    android:layout_marginTop="20dp" 
    android:contentDescription="@string/swap_button_description" 
    android:onClick="swapLocations" 
    android:scaleType="fitStart" 
    android:src="@drawable/swap" /> 

<EditText 
    android:id="@+id/to_location" 
    style="@style/create_trip_activity_components_style" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="20dp" 
    android:ems="10" 
    android:gravity="left" 
    android:hint="@string/to_location_hint" 
    android:imeOptions="actionDone" 
    android:imeActionLabel="Done" 
    android:inputType="text" > 
</EditText> 

<LinearLayout 
    style="@style/create_trip_activity_components_style" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="25dp" 
    android:gravity="center_vertical" 
    android:orientation="horizontal" > 

    <EditText 
     android:id="@+id/departuretime_date" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:clickable="true" 
     android:ems="10" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:gravity="center" 
     android:hint="@string/departure_date_hint" 
     android:inputType="datetime" > 
    </EditText> 

    <EditText 
     android:id="@+id/departuretime_time" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:clickable="true" 
     android:ems="10" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:gravity="center" 
     android:hint="@string/departure_time_hint" 
     android:inputType="datetime" > 
    </EditText> 

</LinearLayout> 

<Button 
    android:id="@+id/pick_match_create_trip" 
    style="@style/big_centered_button_style" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="80dp" 
    android:onClick="pickMatchesButtonClicked" 
    android:text="@string/pick_your_matches" /> 

<TextView 
    style="@style/create_trip_activity_components_style" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="75dp" 
    android:text="@string/current_location_textlabel" 
    android:textAppearance="?android:attr/textAppearanceSmall" /> 

<TextView 
    android:id="@+id/current_location_text" 
    style="@style/create_trip_activity_components_style" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom" 
    android:layout_marginTop="5dp" 
    android:text="" 
    android:textAppearance="?android:attr/textAppearanceSmall" /> 

<ListView 
    android:id="@+id/places_list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="20dp" 
    android:cacheColorHint="#00FFFF" 
    android:background="@color/white" > 
</ListView> 

EditText上從來沒有獲得重新對焦。禁用焦點作品但重新啓用它不會。我也試過用editText2.setEnabled();的方法。它也沒有工作。

+0

請出示editText1和editText2的聲明...... – TN888

+0

你不想焦點或你不希望的EditText可見 – krishna

+0

只是指出調用'setFocusableInTouchMode(假)''後setFocusable(假)'是多餘的。另外,如果調用'setFocusableInTouchMode(true)',則不需要'setFocusable(true)'。 – Vikram

回答

1

試試這個..

editText2.clearFocus(); 
+0

嘗試可聚焦真正的它會使第一可聚焦的對象獲得焦點。這不是我的要求。 – Geek

1

您設置editText2.requestFocus();當過你想專注於EditText

+0

我想讓EditText只關注用戶觸摸。再次,我不想爲此添加onClickListener。 – Geek

+0

試試這個'editText2.isFocusableInTouchMode()' – ved

+0

一旦我設置false,它就會給出錯誤。 – Geek

1

寫在活動清單文件,它由散焦創建活動,在第一次的EditText。並通過觸摸重新獲得焦點編輯文本

android:windowSoftInputMode="adjustPan|stateHidden" 
7

它真的很簡單我的朋友。 您可以嘗試類似下面:

editText1.addTextChangedListener(new TextWatcher() { 
    @Override 
    public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 
     // Set EditText non-focusable 
     editText2.setEnable(false); 
     editText2.setClickable(false); 


    } 
}); 

. 
. // some work 
. 

listView.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 

     // Set EditText focusable 
       editText2.setEnable(true); 
     editText2.setClickable(true); 
} 

只需更新您的代碼像上面和嘗試。那將會起作用。如果沒有,那麼讓我知道,我會幫你。

享受編碼... :)

+0

我試過了。問題是一旦我禁用它,它不會再被啓用。 – Geek

+0

然後,也許在你的鱈魚的其他地方的問題,你是否向我們展示了更多的XML文件? – felixd

+0

@felixd添加了xml文件。事情是,如果我把它啓用EditText的代碼在它的一些方法工作。爲了清楚起見,首先我禁用EditText,然後調用IntentService並將結果返回到活動。要收聽IntentService的結果,我在我的活動中播放了接收器。所以如果我把代碼放在廣播接收器的'onReceive'中或者在應用程序流中的廣播接收器之後的任何其他方法中啓用EditText,這是行不通的。 – Geek

1
Timer timer = new Timer(); 
timer.schedule(new TimerTask() { 

    @Override 
    public void run() { 
    editText2.setEnable(false); 
    editText2.setClickable(false); 
    } 
}, 20); 
1

你可以使用下面的代碼,並通過相應的Java操作。

`<EditText ... 
    android:clickable="false" 
    android:cursorVisible="false" 
    android:focusable="false" 
    android:focusableInTouchMode="false"> 
</EditText>' 
+0

這不提供問題的答案。要批評或要求作者澄清,在他們的帖子下留下評論 - 你總是可以評論你自己的帖子,一旦你有足夠的[聲譽](http://stackoverflow.com/help/whats-reputation),你會能夠[評論任何帖子](http://stackoverflow.com/help/privileges/comment)。 –

+0

你可以使用下面的代碼: – UMESH0492

+0

UMESH0492