gpt4 book ai didi

android - 编辑文本 : Center hint but have entered text start from top

转载 作者:可可西里 更新时间:2023-11-01 19:09:27 25 4
gpt4 key购买 nike

您好,我有一个带有提示的编辑文本,我使用 android:gravity:"center" 将提示设为中间。当我从编辑文本中开始打字时,打字是从中间开始的,如何使打字从左角开始,同时仍然提示居中

<EditText
android:id="@+id/edittext"
android:layout_width="230dp"
android:layout_height="110dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView1"
android:layout_marginLeft="5dp"
android:layout_marginTop="33dp"
android:layout_toLeftOf="@+id/relativeLayout1"
android:background="@drawable/roundcorners"
android:ems="10"
android:gravity="center_horizontal"
android:hint="@string/fbhint"
android:lines="6"
android:paddingRight="5dp"
android:textSize="14sp"
android:windowSoftInputMode="stateHidden" />

最佳答案

我认为这不可能“开箱即用”,但您可以通过编程方式实现:

yourEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 0){
// position the text type in the left top corner
yourEditText.setGravity(Gravity.LEFT | Gravity.TOP);
}else{
// no text entered. Center the hint text.
yourEditText.setGravity(Gravity.CENTER);
}
}
}

关于android - 编辑文本 : Center hint but have entered text start from top,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14461205/

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