Измените пользовательский интерфейс в функции инструкции
-
27-10-2019 - |
Вопрос
Я хочу показать изображение, когда я подключен к серверу (я разрабатываю SIP-приложение).Я сделал это (создал XML-файл [connected.xml] с textview и изображением), но у меня есть FC:
public void onRegistrationDone(String localProfileUri, long expiryTime) {
updateStatus("Registered to server.");
Log.d("SUCCEED","Registration DONE");
setContentView(R.layout.connected);
}
Затем я хочу добавить изображение при подключении и отключении...Как я могу решить эту проблему?Большое вам спасибо.
Редактировать:XML:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView
android:id="@+id/sipLabel"
android:textSize="20sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ViewFlipper
android:id="@+id/flipper"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/disconnected" android:src="@drawable/disconnected" android:layout_below="@id/sipLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="0.35" android:gravity="center"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/connected" android:src="@drawable/connected" android:layout_below="@id/sipLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="0.35" android:gravity="center"
/>
</RelativeLayout>
</ViewFlipper>
</RelativeLayout>
Решение
Вы не должны звонить setContentView
Несколько раз в одном действии, если вы не уверены на 100%, что вы очистили все в нем.
Даже тогда ваши пользователи могут найти странным, что нажав кнопку сзади, они не увидят, чего они ждали, хотя и начинают ту же деятельность.
Так что вам следует думать о
- Изменение видимости ваших представлений, чтобы показать/скрыть его различные части, или
- более элегантное и более простое решение: используйте
ViewFlipper
.
Обновление 1
Вот образец использования ViewFlipper
:
Поместите эти линии в свой макет XML:
<ViewFlipper android:id="@+id/flipper"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- your first view comes here -->
</RelativeLayout>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- your second view comes here -->
</RelativeLayout>
</ViewFlipper>
и в вашем коде Java, когда вам нужно изменить представление, вы пишете:
flipper = (ViewFlipper)findViewById(R.id.flipper);
flipper.showNext();
Дополнительный: Вы можете анимировать свое переворот, все, что вам нужно сделать, это установить in
а также out
анимация вашего flipper
перед звонком showNext
или же showPrevious
:
Animation inAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inAnimation.setDuration(250);
inAnimation.setInterpolator(new AccelerateInterpolator());
Animation outAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outAnimation.setDuration(250);
outAnimation.setInterpolator(new AccelerateInterpolator());
flipper = (ViewFlipper)findViewById(R.id.flipper);
flipper.setInAnimation(inAnimation);
flipper.setOutAnimation(outAnimation);
flipper.showNext();
Обновление 2:
Образец для ViewFlipper
с глобальным заголовком:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:id="@+id/status_text" android:text="Status: "
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<ViewFlipper android:id="@+id/flipper" android:layout_below="@id/status_text"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- your first view comes here without the status TextView -->
</RelativeLayout>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- your second view comes here -->
</RelativeLayout>
</ViewFlipper>
</RelativeLayout>
Другие советы
Вероятно, проблема с потоком:Изменения пользовательского интерфейса должны происходить в основном потоке цикла действия.Посмотрите на http://developer.android.com/reference/android/os/Handler.html:
- Создайте обработчик для действия.
- Отправить сообщение (http://developer.android.com/reference/android/os/Message.html) когда регистрация будет завершена.
- В обработчике получите это сообщение и внесите изменения в свой пользовательский интерфейс.
Дайте мне знать, как это работает.
Редактировать:исправлена ссылка на сообщение