私は反対のシナリオを実行しようとしていました-ヒントが左側に表示され、入力されたテキストが中央に表示されるように、ここで必要な解決策の一部を見つけましたが、これを行うと、上記のシナリオに適合した完全なソリューションを投稿すると思いました:)。
そのため、これを理解しようとすると、2つの課題があることがわかりました。1)ヒントを中央に表示し、ユーザーが入力するテキストはすべて左揃えにします。2)ヒントが始まる場所ではなく、ユーザーのテキストが表示される場所にカーソルがすぐに表示されるようにします。また、ユーザーが入力を開始するまでヒントを残し、ユーザーが入力内容を削除したときに再び表示されるようにしたかったのです。
私のソリューションでは、2つのEditTextボックスを重ねて表示し、上のボックスにはヒントを、下のボックスにはテキストを含めました。そのように:
<!-- This EditText box is where the user writes their text.
It is aligned left, doesn't have a hint in it,
and is the desired width of the box
!-->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textShortMessage"
android:fontFamily="sans-serif-medium"
android:background="@color/white"
android:layout_marginTop="5dp"
android:id="@+id/event_heading"
android:hint=""
android:layout_below="@id/page_title"
/>
<!-- This second EditText box only contains the hint text.
It is centered, is only the width of the hint text,
and for my purposes, the text was italic !-->
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textShortMessage"
android:fontFamily="sans-serif-medium"
android:background="@color/white"
android:layout_marginTop="5dp"
android:layout_below="@id/page_title"
android:id="@+id/event_headingHint"
android:hint="Heading"
android:gravity="center"
android:textStyle="italic"
/>
ヒントを含むボックスは2番目で、他のボックスの上に表示されました。これをフラグメント内で使用していたので、フラグメント内のonCreateViewをオーバーライドし、2つのEditTextボックスを引き出して、必要なリスナーを追加するために作成したメソッドに次のように送信しました。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_create_event, container, false);
EditText heading = (EditText) view.findViewById(R.id.event_heading);
EditText headingHint = (EditText) view.findViewById(R.id.event_headingHint);
addFormatTidier(heading, headingHint);
return view;
}
それ以外の場合、フラグメントではなくアクティビティ内でこれを実行しようとすると、setContentViewコマンドの直後にあるonCreateメソッド内で同じコードを使用できます。
@Override
protected void onCreate(Bundle savedInstanceState)
{
...
setContentView(R.layout.activity_add_event);
EditText heading = (EditText) findViewById(R.id.event_heading);
EditText headingHint = (EditText) findViewById(R.id.event_headingHint);
addFormatTidier(heading, headingHint);
...
}
次に、最後に'addFormatTidier'メソッドで、各EditTextボックスにリスナーを追加しました。最初にonFocusChangeリスナーを'hint' EditTextにアタッチし、ユーザーがヒントをクリックした場合にフォーカスを他のEditTextに移動しました。次に、ユーザーが入力を開始するとヒントを削除し、ユーザーが入力したものをすべて削除した場合にヒントを復元するために、addTextChangedListenerを他のEditTextにアタッチしました。
private void addFormatTidier(final EditText text, final EditText hint)
{
// save the hint so I can restore it later
final CharSequence hintText = hint.getHint();
// Add a listener to shift focus away from the hint text to the other EditText when the hint is clicked.
hint.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
{
text.requestFocus();
}
}
});
// Add a listener to remove the hint text when the user starts typing, and to restore it when the user clears what they have typed.
text.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)
{
hint.setHint("");
}
else
{
hint.setHint(hintText);
}
}
});
}
ユーザーがフィールドをクリックするとすぐにヒントが消え、テキストを残さずにクリックすると再び表示されるようにするには、代わりに次の方法を使用できます。
private void addFormatTidier(final EditText text, final EditText hint)
{
// save the hint so I can restore it later
final CharSequence hintText = hint.getHint();
// Add a listener to shift focus away from the hint text to the other EditText when the hint is clicked, and simultaneously clear the hint text.
hint.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
{
text.requestFocus();
hint.setHint("");
}
}
});
// Add a listener to remove the hint text when the main EditText receives focus, and to restore it when the EditText loses focus without any text inside it.
text.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
{
hint.setHint("");
}
else
{
if(!(text.getText().length()>0))
{
hint.setHint(hintText);
}
}
}
});
}
これが、同様の問題を解決する方法を見つけようとしている他の人に役立つことを願っています:D。上記の彼のコメントのuserM1433372が、私をどこから始めるべきかについて正しい軌道に乗せたのです:D。