質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

1075閲覧

android textclockの切り替えについて

bake

総合スコア13

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2019/06/29 17:22

プログラミング初心者です。
12hと24Hの時計をTextClockで表示するレイアウトを作成したのですが、
ボタンを押すと(画面遷移はせずに)12Hと24Hが切り替わるようにしたいです。
どのような実装をすればよいかわかりません。よろしくお願いします。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/blue">

<TextView android:id="@+id/TextView01" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="110dp" android:layout_marginTop="10dp" android:textColor="#000000" android:textSize="20dp" /> <TextClock android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="110dp" android:layout_marginTop="10dp" android:textSize="50dp" android:textColor="#ff00ff" android:format12Hour="kk:mm:ss"

  <TextClock android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="110dp" android:layout_marginTop="10dp" android:textSize="50dp" android:textColor="#ff00ff" android:format24Hour="a hh:mm:ss"/>

<Button android:id="@+id/tH" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="12H" android:onClick="onButtonClick" android:textSize="16sp" />
</LinearLayout>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

以下のふたつの方法がとりあえず考えられるかと思います。

1.TextClockをひとつにしてformatをコードでセットし直す。

java

1boolean flag = true; 2 3public void onButtonClick(View v) { 4 5 TextClock textClock = findViewById(R.id.TextView02); 6 7 if(flag) { 8 textClock.setFormat12Hour(null); 9 textClock.setFormat24Hour("a hh:mm:ss"); 10 } else { 11 textClock.setFormat12Hour("kk:mm:ss"); 12 textClock.setFormat24Hour(null); 13 } 14 15 flag = !flag; 16}

xml

1 <TextClock 2 android:id="@+id/TextView02" 3 android:layout_width="wrap_content" 4 android:layout_height="wrap_content" 5 android:layout_marginLeft="110dp" 6 android:layout_marginTop="10dp" 7 android:textSize="50dp" 8 android:textColor="#ff00ff" 9 android:format12Hour="kk:mm:ss"/>

2.TextClockをふたつ用意して重ねておいてvisibilityを切り替える。

Java

1boolean flag = true; 2 3public void onButtonClick(View v) { 4 TextClock textClock2 = findViewById(R.id.TextView02); 5 TextClock textClock3 = findViewById(R.id.TextView03); 6 7 if(flag) { 8 textClock2.setVisibility(View.GONE); 9 textClock3.setVisibility(View.VISIBLE); 10 } else { 11 textClock2.setVisibility(View.VISIBLE); 12 textClock3.setVisibility(View.GONE); 13 } 14 15 flag = !flag; 16} 17

xml

1 <FrameLayout 2 android:layout_width="wrap_content" 3 android:layout_height="wrap_content" 4 > 5 <TextClock 6 android:id="@+id/TextView02" 7 android:layout_width="wrap_content" 8 android:layout_height="wrap_content" 9 android:layout_marginLeft="110dp" 10 android:layout_marginTop="10dp" 11 android:textSize="50dp" 12 android:textColor="#ff00ff" 13 android:format12Hour="kk:mm:ss"/> 14 <TextClock 15 android:id="@+id/TextView03" 16 android:visibility="gone" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:layout_marginLeft="110dp" 20 android:layout_marginTop="10dp" 21 android:textSize="50dp" 22 android:textColor="#ff00ff" 23 android:format24Hour="a hh:mm:ss"/> 24 /> 25 </FrameLayout>

以上、不明点等あればコメントお願いいたします。

投稿2019/07/02 12:23

編集2019/07/02 12:24
razuma

総合スコア1313

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

bake

2019/07/02 12:32

ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問