暂时禁用EditText输入框

5

我希望暂时让EditText失去焦点。

场景是:我有两个EditText。每当任何一个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();方法,但它也不起作用。


请展示editText1和editText2的声明... - TN888
你不想让焦点或EditText可见。 - krishna
只是指出在调用setFocusable(false)之后再调用setFocusableInTouchMode(false)是多余的。另外,如果你调用了setFocusableInTouchMode(true),则不需要调用setFocusable(true) - Vikram
你能否在 onEditTextChanged 事件中将 focusable 设为 false?如果可以,那么你可以尝试使用 Handler 和 onPostDelayed 方法将其设为 true,并设置延迟时间为 2000 毫秒。我在类似的情况下使用过这种方法。 - Manmohan Badaya
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);
}

只需更新你的代码,类似上面的样子,然后尝试一下。那肯定会起作用的。如果不行,请告诉我,我会帮助你。

享受编码的乐趣吧...:)


我试过了。问题是一旦我禁用它,它就无法再次启用。 - Geek
那么也许问题在你的代码的其他地方,你想展示更多的内容吗,比如XML文件? - felixd
@felixd 添加了xml文件。问题是,如果我将启用EditText的代码放在某些方法中,它就会起作用。为了明确起见,首先我使EditText无效,然后调用IntentService并将结果返回到活动中。为了监听IntentService的结果,我在我的活动中有广播接收器。因此,如果我将启用EditText的代码放在广播接收器的onReceive或在应用程序流程中广播接收器之后的任何其他方法中,这将不起作用。 - Geek
@Akash,听起来像是一个糟糕的接线问题或者一个永远不会被执行的代码块...请添加你的整个代码块,包括意图请求和广播接收器,因为似乎你的问题就出在那里。 - Mortalus

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

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

1

将其写入活动的清单文件中,这样在首次创建活动时,它会使EditText失去焦点。通过触摸EditText重新获得焦点。

android:windowSoftInputMode="adjustPan|stateHidden"

1
你可以使用以下代码,并通过Java进行相应的操作。
`<EditText ...
    android:clickable="false" 
    android:cursorVisible="false" 
    android:focusable="false" 
    android:focusableInTouchMode="false">
 </EditText>'

这并没有提供问题的答案。如果要批评或请求作者澄清,请在他们的帖子下留言 - 您始终可以在自己的帖子上发表评论,并且一旦您拥有足够的声望,您将能够评论任何帖子 - Saullo G. P. Castro
<EditText ... android:clickable="false" android:cursorVisible="false" android:focusable="false" android:focusableInTouchMode="false"> </EditText> - UMESH0492

1

try this..

editText2.clearFocus();

它将使第一个可聚焦的对象获得焦点。这不是我的要求。 - Geek

1

当您想要将焦点放在EditText上时,可以使用editText2.requestFocus();


我希望EditText只有在用户触摸时才获得焦点。再次强调,我不想仅为此目的添加onClickListener。 - Geek
一旦我设置为false,它就会返回false。 - Geek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接