android - Handle multiple view clicks

activity_main.xml
 <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:id="@+id/coordinator_layout"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="#b8cfb2"     >     <LinearLayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:orientation="horizontal"         android:background="#b8cfb2"         >         <Button             android:id="@+id/btn_red"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="RED"             />         <Button             android:id="@+id/btn_green"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="Green"             />         <TextView             android:id="@+id/tv_yellow"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="YELLOW"             />         <Button             android:id="@+id/btn_blue"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="Blue"             />         <TextView             android:id="@+id/tv_black"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="BLACK"             />     </LinearLayout> </android.support.design.widget.CoordinatorLayout> 
MainActivity.java
 package com.cfsuman.me.androidcodesnippets;  import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.support.design.widget.CoordinatorLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView;   public class MainActivity extends AppCompatActivity implements View.OnClickListener {     private Context mContext;     private Activity mActivity;      private CoordinatorLayout mCLayout;     private Button mButtonRed;     private Button mButtonGreen;     private Button mButtonBlue;      private TextView mTextViewYellow;     private TextView mTextViewBlack;       @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          // Get the application context         mContext = getApplicationContext();         mActivity = MainActivity.this;          // Get the widget reference from XML layout         mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);         mButtonRed = (Button) findViewById(R.id.btn_red);         mButtonGreen = (Button) findViewById(R.id.btn_green);         mButtonBlue = (Button) findViewById(R.id.btn_blue);         mTextViewYellow = (TextView) findViewById(R.id.tv_yellow);         mTextViewBlack = (TextView) findViewById(R.id.tv_black);          // Set click listener for buttons         mButtonRed.setOnClickListener(this);         mButtonGreen.setOnClickListener(this);         mButtonBlue.setOnClickListener(this);          // Set click listener for text views         mTextViewYellow.setOnClickListener(this);         mTextViewBlack.setOnClickListener(this);     }      @Override     public void onClick(View v){         if(v.getId() == R.id.btn_red){             mCLayout.setBackgroundColor(Color.RED);         }else if(v.getId() == R.id.btn_green){             mCLayout.setBackgroundColor(Color.GREEN);         }else if(v.getId() == R.id.btn_green){             mCLayout.setBackgroundColor(Color.GREEN);         }else if(v.getId() == R.id.btn_blue){             mCLayout.setBackgroundColor(Color.BLUE);         }else if(v.getId() == R.id.tv_yellow){             mCLayout.setBackgroundColor(Color.YELLOW);         }else if(v.getId() == R.id.tv_black){             mCLayout.setBackgroundColor(Color.BLACK);         }     } } 

Komentar