1

こんにちは、こんにちはワールド アプリに関して別の質問があります。A ボタンが押されたときに背景を変更したいので、次のようにしました。

    public void onclick01(View View)  
       {  
           View.setBackgroundColor(Color.GREEN);

       } 

ただし、それはアプリ全体ではなく、ボタンの背景色を変更します。


編集

あと2つ質問があります。

1)どのように設定しますか

View.setBackgroundColor(Color.GREEN);

次のようなものに:

View.setBackgroundColor(Color.RANDOM);

2) テキストの色を変更するにはどうすればよいですか? 何かのようなもの:

View.setTextColor(Color.GREEN);?
4

6 に答える 6

1

main_act.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button" />

</LinearLayout>

アクティビティ

public class MainActivity extends Activity {
/** Called when the activity is first created. */
Button b1;
LinearLayout layout;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_act);
    layout=(LinearLayout)findViewById(R.id.layout);
    blueButton=(Button)findViewById(R.id.b1);
    b1.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
        // TODO Auto-generated method stub
        layout.setBackgroundColor(Color.BLUE);

    }
});
}
}
于 2013-03-02T04:21:57.883 に答える
0

使用Selectorすると、ボタンを押すアクションが得られます。

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:drawable="@drawable/button_background" android:state_pressed="false"/>
 <!-- default -->
 <item android:drawable="@drawable/button_pressed" android:state_pressed="true"/>
 <!-- pressed -->
</selector>
于 2013-03-02T04:36:50.913 に答える
0

xml で設定する場合は、次のようにする必要があります。

   android:background="@android:color/green"

Androidのデフォルトのカラーコードを使用することにした場合、またはcolors.xmlで指定された色がある場合は、使用します

    android:background="@colors/green"

プログラムで実行する場合は、次のようにします。

     LinearLayout linearlayout=(LinearLayout) findViewById(R.layout.yourlayout);
     linearlayout.setBackgroundColor(Color.GREEN);
于 2013-03-02T04:20:40.113 に答える
0

ここでビューはあなたのボタンのビューを参照します。したがって、親レイアウトのオブジェクトを作成してから作成する必要があります

layout.setBackgroundColor(Color.GREEN);
于 2013-03-02T04:21:52.330 に答える
0
public void setActivityBackgroundColor(int color) {
 view = this.getWindow().getDecorView();
 view.setBackgroundColor(color);
}

次に、必要な色を渡して OnClickListener から呼び出します。

于 2013-03-02T04:26:09.690 に答える
0

この状況ではxmlを使用する必要があります

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:state_pressed="true">
    <shape android:shape="rectangle">
       <solid android:color="yourColor"/>
    </shape>
</item>

<item>
    <shape android:shape="rectangle">
        <solid android:color="yourColor"/>
    </shape>
</item>

</selector>
于 2013-03-02T04:28:41.747 に答える