starałem się zrobić odwrotny scenariusz - dla aluzja do stawienia się na w lewo, a napisany tekst pojawił się w środku i znalazłem część rozwiązania, której potrzebowałem, ale dla dobra innych osób, które próbują to zrobić, pomyślałem, że opublikuję pełne rozwiązanie, dostosowane do powyżej scenariusz :).
Tak jak próbowałem to rozgryźć, okazało się, że były dwa wyzwania: 1) Aby wskazówka pojawiła się na środku, podczas gdy dowolny tekst, który użytkownik wpisze, jest wyrównany do lewej. 2) Aby kursor natychmiast pojawił się w miejscu, w którym pojawi się tekst użytkownika, a nie tam, gdzie zaczyna się wskazówka. Chciałem również, aby wskazówka pozostała, dopóki użytkownik nie zaczął pisać, i aby pojawiła się ponownie, gdy użytkownik usunął to, co wpisał.
Moje rozwiązanie zawierało dwa pola EditText ułożone jeden na drugim, z górnym okienkiem zawierającym podpowiedź i dolnym zawierającym tekst. tak:
<!-- 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"
/>
Pudełko z podpowiedź było drugie, tak, że pojawił się nad drugim. używałem tego obrębie fragmentu więc wtedy overrode onCreateView we fragmencie, wyciągnął dwa pudełka EditText i wysłał je do metody napisałem do dodawania niezbędnych słuchaczy następująco:
@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;
}
W przeciwnym razie, jeśli starając się zrobić to w ramach działalności zamiast fragmentu, można użyć tego samego kodu wewnątrz metody onCreate, tuż po komendzie setContentView jako tak:
@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);
...
}
wreszcie do mojego „addFormatTidier” metody, dodałem Listener do każdego pola EditText: Najpierw załączam detektor onFocusChange do EditPext 'podpowiedzi', mo ve skupić się na innym EditText, jeśli użytkownik kliknął wskazówkę.Potem załączeniu addTextChangedListener do mojej drugiej EditText, w celu usunięcia wskazówkę gdy użytkownik rozpoczął pisanie i przywrócić go, gdy użytkownik usunął wszystko, co wpisane, jak następuje:
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);
}
}
});
}
Dla wskazówkę zniknąć tak szybko, jak użytkownik kliknie na boisku i ponownie jeśli kliknąć bez wychodzenia dowolny tekst w środku, następujące metody mogą być stosowane zamiast:
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);
}
}
}
});
}
mam nadzieję, że jest to przydatne do nikogo innego próbuje dowiedzieć się, jak rozwiązać podobny problem: D. Był to użytkownikM1433372 w powyższym komentarzu, który ustawił mnie na właściwej ścieżce, gdzie zacząć: D.
Chcę, aby wskazówka pozostała w centrum ... za pomocą androida: gravity = "góra | w lewo" podpowiedź również w lewym górnym rogu. – teekib