5

我想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();方法。它也没有用。

4

6 回答 6

8

它真的很简单我的朋友。您可以尝试以下方法:

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

只需像上面一样更新您的代码并尝试。这将非常有效。如果没有,请告诉我,我会帮助你。

享受编码... :)

于 2013-08-09T10:26:27.587 回答
2
Timer timer = new Timer();
timer.schedule(new TimerTask() {

  @Override
  public void run() {
    editText2.setEnable(false);
    editText2.setClickable(false);
  }
}, 20);
于 2013-08-09T10:55:25.230 回答
1

您可以使用以下代码并通过 java 进行相应操作。

`<EditText ...
    android:clickable="false" 
    android:cursorVisible="false" 
    android:focusable="false" 
    android:focusableInTouchMode="false">
 </EditText>'
于 2013-08-10T16:42:50.900 回答
1

写在activity的manifest文件中,第一次通过创建activity来散焦edittext。并通过点击编辑文本重新获得焦点

android:windowSoftInputMode="adjustPan|stateHidden"
于 2013-08-02T12:26:36.347 回答
1

尝试这个..

editText2.clearFocus();
于 2013-08-02T11:55:07.107 回答
1

您可以设置editText2.requestFocus();何时想要专注于EditText.

于 2013-08-02T11:55:44.957 回答