2017-05-10 10 views
0

私はただ、今週Udacityのコースへの感謝をXMLとJavaコードを習い始め...私はやっと途中だが、私は自分の無意味なアプリを作るように感じた。..単一のTextViewでクリック可能なループを作成しますか?

だから私はいくつかの黒のテキストを持っています赤い背景に。

テキストをクリックすると、テキストを白と背景に青に変更するようJavaに指示します。

テキストをもう一度クリックすると、それを黒と赤に戻したいが、そうはしない。

私はこれがなぜ起こるのか知っていますが、これを修正する方法はわかりません。 私のテキストには、色を変更して画面に表示するためにonClickがあるために起こります。したがって当然、クリックごとに同じonClickが呼び出されています。

クリックするたびに2段階の色を常に交互に切り替えるにはどうすればよいですか?

私のXMLは次のとおりです。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#c04" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/textDisplay" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center_horizontal" 
     android:layout_weight="1" 
     android:background="#c04" 
     android:fontFamily="sans-serif-smallcaps" 
     android:gravity="center" 
     android:onClick="Hey" 
     android:padding="30dp" 
     android:text="Hey!" 
     android:textAllCaps="true" 
     android:textColor="#000" 
     android:textSize="150sp" 
     android:textStyle="bold" /> 

</LinearLayout> 

とJavaコードが

public void displaying(String message) { 
    TextView whatever = (TextView) findViewById(R.id.textDisplay); 
    whatever.setText(message); 
    whatever.setTextColor(Color.rgb(255, 255, 255)); 
    whatever.setBackgroundColor(Color.rgb(82, 218, 199)); 
} 

public void Hey(View v) { 
    displaying("Ho!"); 
} 

である私は、いくつかの掘削を行なったし、私は "onClickListener" を使用することができましたか?しかし、私が何をしても、私はそれを働かせることができませんでした。たぶん私は構文の権利を得ることができません。 別の方法がありますか?おそらく簡単ですか?それは私の頭の中でかなり簡単な仕事のように思えるが、これは本当に私に困惑している。

答えて

0

次の操作を行います。

public void Hey(View v) { 
    TextView whatever = (TextView) findViewById(R.id.textDisplay); 
    if(whatever.getText().toString().equals("Hey!")){ 
     whatever.setText("Ho!"); 
     whatever.setTextColor(Color.rgb(255, 255, 255)); 
     whatever.setBackgroundColor(Color.rgb(82, 218, 199)); 
    } else if (whatever.getText().toString().equals("Ho!"){ 
     whatever.setText("Hey!"); 
     whatever.setTextColor(Color.rgb(0, 0, 0)); 
     whatever.setBackgroundColor(Color.rgb(/*red RGB*/)); 
    } 
} 

あなたはウィッヒのテキストとあなたのTextViewのテキストに基づいて設定された背景色を決定します。

+0

これは機能しました。ありがとう!私は に配置しなければならなかったものの、多くのことを感謝し ます。public voidは、(文字列のメッセージ) ない ます。public voidねえ(ビューV) を表示するしかし、それはトリックをしました!私はいくつかのif/elseのものを見上げるだろうと思う! – GuitarGuy

0

表示の状態をブール値に保つことができます。そのようなもの(未テスト):

boolean stateOfMyTextView = true; 

public void displaying(String message) { 
    TextView whatever = (TextView) findViewById(R.id.textDisplay); 
    whatever.setText(message); 
    if (stateOfMyTextView == true) { 
     // First color 
     whatever.setTextColor(Color.rgb(255, 255, 255)); 
     whatever.setBackgroundColor(Color.rgb(82, 218, 199)); 
    } 
    else { 
     // Other color 
     whatever.setTextColor(Color.rgb(0, 0, 0)); 
     whatever.setBackgroundColor(Color.rgb(255, 0, 0)); 
    } 
    // change state of the boolean 
    stateOfMyTextView = !stateOfMyTextView; 
} 
関連する問題