简体   繁体   English

单击另一个TextView时如何向EditText添加焦点

[英]How do I add focus to a EditText when clicking on another TextView

I'm using a hidden EditText (visibility is not set to invisible, but rather the EditText has 0dp width and height) to receive user input. 我正在使用隐藏的EditText(可见性未设置为不可见,而是EditText的宽度和高度为0dp)来接收用户输入。 I'm using the input data to fill other TextViews. 我正在使用输入数据填充其他TextView。 The reason i'm doing this is because I don't want the visible forms (the TextViews) to have the same properties as an actual EditText, but I do want to use the Soft Keyboard. 我这样做的原因是因为我不希望可见的窗体(TextViews)具有与实际EditText相同的属性,但是我确实想使用软键盘。

My problem is that when the user chooses to hide the keyboard, either by pressing back or the 'done' button, I want to make it reappear when they click on a TextView, so that they once again can start editing the hidden EditText. 我的问题是,当用户选择隐藏键盘时,可以通过按返回键或“完成”按钮来实现,我想使它们在单击TextView时重新出现,以便他们可以再次开始编辑隐藏的EditText。

I've tried the following code, with no success: 我尝试了以下代码,但没有成功:

if(hiddenEt.requestFocus()) {
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}

The code within the if-statement does run, but the Soft Keyboard doesn't reappear. if语句中的代码可以运行,但是软键盘不会重新出现。

Is there a separate function for actually calling the Soft Keyboard? 实际调用软键盘是否有单独的功能?

Try the following code inside textview's on click.. 在click上尝试以下代码。

hiddenEt.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(hiddenEt, InputMethodManager.SHOW_IMPLICIT);
InputMethodManager imm=(InputMethodManager)getSystemService(yourActivity.this.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

Add android:windowSoftInputMode="stateVisible|adjustResize|adjustPan" in your manifest file. 在清单文件中添加android:windowSoftInputMode="stateVisible|adjustResize|adjustPan"

<activity
            android:name=".yourActivity"
            android:configChanges="keyboardHidden|orientation"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateVisible|adjustResize|adjustPan" >
        </activity>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM