gpt4 book ai didi

Android 4.0 EditText 无软键盘带光标定位

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:16:14 26 4
gpt4 key购买 nike

我正在尝试定义一个 EditText 框,而不是在触摸框时自动显示软键盘。我还需要根据触摸显示和移动闪烁的光标。在 Android 4.0 之前,只需使用 mText.setInputType(InputType.TYPE_NULL) 即可轻松完成此操作。这是抑制自动软键盘显示的唯一方法,但在 Android 4.0 中它也抑制了闪烁的光标。但是,光标确实定位正确,并且 mText.getSelectionStart() 确实返回了最后一个触摸位置。例如,如果我在包含“123”的 EditText 框中触摸“2”和“3”,即使没有显示光标,mText.getSelectionStart() 也会正确返回 2。有没有办法以编程方式在该位置显示光标?

这是我用来测试 EditText 光标位置的代码:

public class TestCodeActivity extends Activity implements OnClickListener {
private EditText mText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getApplicationContext();
setContentView(R.layout.test);
findViewById(R.id.Button01).setOnClickListener(this);
findViewById(R.id.Button02).setOnClickListener(this);
findViewById(R.id.Button03).setOnClickListener(this);
findViewById(R.id.Button04).setOnClickListener(this);
mText = (EditText) findViewById(R.id.editText1);
mText.setInputType(InputType.TYPE_NULL);
}

@Override
public void onClick(View v) {
if (v.getTag().equals("Clear")) {
mText.setText("");
} else {
String buttontag = v.getTag().toString();
String str = mText.getText().toString();
int cursor = mText.getSelectionStart();
if(cursor==str.length())
str = str + buttontag;
else
str = str.substring(0, cursor) + buttontag + str.substring(cursor, str.length());
mText.setText(str);
mText.setSelection(cursor+1);
// ---> need code to display blinking cursor at cursor+1 location ???
}
}
}

这是布局 xml:

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="85dp" >
<EditText
android:id="@+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.13"
android:ems="10"
android:textSize="25sp" >
<requestFocus />
</EditText>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/Button01"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="1"
android:tag="1"
android:layout_gravity="center"
android:textSize="30sp" />
<Button
android:id="@+id/Button02"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="2"
android:tag="2"
android:layout_gravity="center"
android:textSize="30sp" />
<Button
android:id="@+id/Button03"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="3"
android:tag="3"
android:layout_gravity="center"
android:textSize="30sp" />
<Button
android:id="@+id/Button04"
android:layout_width="60dp"
android:layout_height="60dp"
android:text="Clear"
android:tag="Clear"
android:layout_gravity="center"
android:textSize="30sp" />
</LinearLayout>

</LinearLayout>

最佳答案

我终于想出了如何抑制软键盘并仍然让闪烁的光标显示在 EditText 框中。我编写了一个 onTouchListener 并返回 true 以禁用键盘而不是使用“mText.setInputType(InputType.TYPE_NULL)”。然后我必须获得触摸位置以将光标设置到正确的位置。

这是我使用的代码:

  mText = (EditText) findViewById(R.id.editText1);
OnTouchListener otl = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Layout layout = ((EditText) v).getLayout();
float x = event.getX() + mText.getScrollX();
int offset = layout.getOffsetForHorizontal(0, x);
if(offset>0)
if(x>layout.getLineMax(0))
mText.setSelection(offset); // touch was at end of text
else
mText.setSelection(offset - 1);
break;
}
return true;
}
};
mText.setOnTouchListener(otl);

关于Android 4.0 EditText 无软键盘带光标定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13811080/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com