0

ボタンをクリックしたときにビューの背景の色を変更しようとしていますが、現在持っているコードではアプリが開きません。何が問題なのかわかりません..

コード:

package on.click.button;

import android.support.v7.app.ActionBarActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class MainActivity extends ActionBarActivity {

        private ImageButton imagebutton;
        private ImageButton imagebuttonRED;
        private ImageButton imagebuttonBLUE;
        private ImageButton imagebuttonYELW;
        private ImageButton imagebuttonGRN;
        private View layout;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            imagebuttonRED = (ImageButton) findViewById(R.id.imageButton1);
            imagebuttonBLUE = (ImageButton) findViewById(R.id.imageButton3);
            imagebuttonYELW = (ImageButton) findViewById(R.id.imageButton2);
            imagebuttonGRN = (ImageButton) findViewById(R.id.imageButton4);

            layout = (View) findViewById(R.layout.activity_main);

            imagebutton.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    setContentView(R.layout.activity_main);
                }
            });

            imagebuttonRED.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                        setContentView(layout);
                    layout.setBackgroundColor(Color.GREEN);
                }
            });

            imagebuttonBLUE.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                        setContentView(layout);
                    layout.setBackgroundColor(Color.RED);
                }
            });
            imagebuttonYELW.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                        setContentView(layout);
                    layout.setBackgroundColor(Color.YELLOW);
                }
            });
            imagebuttonGRN.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                        setContentView(layout);
                    layout.setBackgroundColor(Color.BLUE);
                }
            });

}
}

main_activity.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="nl.aventus.delaatstestap.MainActivity" >

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageButton2"
        android:layout_marginLeft="14dp"
        android:layout_toRightOf="@+id/imageButton2"
        android:src="@drawable/blauw" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:src="@drawable/rood" />

    <ImageButton
        android:id="@+id/imageButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageButton2"
        android:layout_below="@+id/imageButton2"
        android:src="@drawable/flash" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageButton1"
        android:layout_centerHorizontal="true"
        android:src="@drawable/groen" />

    <View
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageButton4"
        android:layout_centerHorizontal="true"
        android:background="#FF0000" />

    <ImageButton
        android:id="@+id/imageButton5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/view1"
        android:layout_alignRight="@+id/imageButton1"
        android:layout_marginRight="19dp"
        android:src="@drawable/min" />

    <ImageButton
        android:id="@+id/imageButton6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/view1"
        android:layout_alignLeft="@+id/imageButton3"
        android:layout_marginLeft="18dp"
        android:src="@drawable/plus" />

</RelativeLayout>

助けてください!

4

5 に答える 5

2

drawable に backcolor.xml を定義します。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" 
     android:drawable="@drawable/clicked" />
<item  android:state_focused="false" 
  android:drawable="@drawable/normal" />
</selector>

ドローアブルの normal.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#FFFFFF"/>    
</shape>

ドローアブルの Clicked.xml

 <?xml version="1.0" encoding="UTF-8"?> 
 <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
 <solid android:color="#FF1A47"/>      
 </shape>

レイアウトに背景を設定する

android:background="@drawable/background"

そしてOnclicklistner

 ll.setBackgroundColor(Color.RED);
于 2014-11-10T10:46:59.497 に答える
0

問題はこの行のここにあるようです

 layout = (View) findViewById(R.layout.activity_main);

xml ファイルで ViewById を検索しようとしています。したがって、Activity_main で親に ID を与え、この行でそのビューを見つけるだけです。

RelativeLayout layout = (RelativeLayout) findViewById(R.layout.yourParentId);// since the parent is RelativeLayout

背景色を変更します

于 2014-11-10T10:44:25.047 に答える
0

2 つの変更を行う必要があります。手順に従ってください。

ステップ 1 :メイン アクティビティのRelativeLayoutにidを追加する

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="nl.aventus.delaatstestap.MainActivity" >

ステップ 2 : その ID を使用して背景色を変更します。

relLayout = (View) findViewById(R.layout.rootLayout);
//to change color
relLayout.setBackgroundColor(Color.GREEN);
于 2014-11-10T10:43:09.840 に答える
0

まず、ID を XML で RelativeLayout に割り当てます。

android:id="@+id/rlyt"

次に、Java ファイルの 31 行目を修正します。つまり、

            layout = (View)findViewById(R.layout.activity_main); 

            layout = (View)findViewById(R.id.rlyt);

OnClick で setContentView を使用しないでください

于 2014-11-10T11:08:50.877 に答える
-1

このように RelativeLayout に Id を入れます

                <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:id="@+id/viewId"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:paddingBottom="@dimen/activity_vertical_margin"
                  android:paddingLeft="@dimen/activity_horizontal_margin"
                  android:paddingRight="@dimen/activity_horizontal_margin"
                  android:paddingTop="@dimen/activity_vertical_margin"
                  tools:context="nl.aventus.delaatstestap.MainActivity" >

そしてあなたのアクティビティでこれを行います:

                  RelativeLayout  layout = (RelativeLayout) findViewById(R.id.viewId);
                  ImageButton imagebutton = (ImageButton)findViewById(R.id......);   

                  imagebutton.setOnClickListener(new OnClickListener() {

                  public void onClick(View v) {
                         setContentView(R.layout.activity_main);
            }
        });
于 2014-11-10T10:58:14.470 に答える