2013-08-02 4 views
5

Voglio che lo EditText non guadagni temporaneamente.Disattivazione temporanea EditText

Lo scenario è: Ho due EditTexts. Ogni volta che viene modificato qualsiasi testo di EditText, voglio che un altro EditText non risponda ai clic dell'utente fino a quando non si verifica un altro evento predefinito.

Per raggiungere questo ho provato questo:

@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); 
} 

Disposizione:

<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 non riceve lo più. Disabilitare la messa a fuoco funziona ma abilitarla di nuovo no. Ho anche provato a utilizzare il metodo editText2.setEnabled();. Non ha funzionato, anche.

+0

Si prega di mostrare dichiarazione di editText1 e editText2 ... – TN888

+0

non volete la messa a fuoco o non volete l'EditText sia visibile – krishna

+0

solo sottolineando che la chiamata 'setFocusableInTouchMode (false)' 'dopo setFocusable (false)' è ridondante. Inoltre, 'setFocusable (true)' non è richiesto se si chiama 'setFocusableInTouchMode (true)'. – Vikram

risposta

1

provare questo ..

editText2.clearFocus(); 
+0

Farà focalizzare il primo oggetto focalizzabile . Questo non è il mio requisito. – Geek

1

si imposta editText2.requestFocus(); quando mai si desidera mettere a fuoco EditText.

+0

Desidero che EditText si concentri solo sul tocco dell'utente. Ancora una volta non voglio aggiungere onClickListener solo per questo scopo. – Geek

+0

Prova questo 'editText2.isFocusableInTouchMode()' – ved

+0

Fornisce false una volta impostato false. – Geek

1

scrivilo in un file manifest di attività, defocalizza l'edittoxt per attività di creazione in un primo momento. e riottenere lo stato attivo toccando Modifica testo

android:windowSoftInputMode="adjustPan|stateHidden" 
7

È davvero semplice amico mio. Si può provare qualcosa di simile di seguito:

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); 
} 

Basta aggiornare il codice di qualcosa come sopra e provare. che funzionerà sgarbatamente. se no allora fammi sapere, ti aiuterò.

Godetevi Coding ... :)

+0

L'ho provato. Il problema è una volta che lo disattivo, non viene abilitato di nuovo. – Geek

+0

Allora forse il problema in qualche altra parte del tuo cod, ti bacchetta a mostrarci di più, come il file XML? – felixd

+0

@felixd Aggiunto il file xml. Il fatto è che se metto il codice di abilitazione di EditText in alcuni metodi funziona. Per chiarire, per prima cosa faccio in modo che EditText sia disattivato, quindi chiamiamo un IntentService e restituiamo i risultati all'attività. Per ascoltare il risultato di IntentService ho un ricevitore broadcast nella mia attività.Quindi, se inserisco il codice per abilitare EditText in "onReceive" del ricevitore di trasmissione o in qualsiasi altro metodo dopo il destinatario della trasmissione nel flusso di app, questo non funziona. – Geek

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

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

è possibile utilizzare il seguente codice e manipolare conseguenza da Java.

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

Questo non fornisce una risposta alla domanda. Per criticare o richiedere chiarimenti da un autore, lascia un commento sotto il loro post - puoi sempre commentare i tuoi post, e una volta che hai [reputazione] sufficiente (http://stackoverflow.com/help/whats-reputation) essere in grado di [commentare qualsiasi post] (http://stackoverflow.com/help/privileges/comment). –

+0

è possibile utilizzare il seguente codice: – UMESH0492

+0

UMESH0492