用觸控語法「onTouch」來執行。
如下紅色表示:
package com.example.fan.text6; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; public class Text6Activity extends AppCompatActivity { Button b2,b3,b4,b5,b6,b11,b12,b13,b14; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_text6); AdView mAdView = (AdView)findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest); b2=(Button)findViewById(R.id.button2); b3=(Button)findViewById(R.id.button3); b4=(Button)findViewById(R.id.button4); b5=(Button)findViewById(R.id.button5); b6=(Button)findViewById(R.id.button6); b11=(Button)findViewById(R.id.button11); b12=(Button)findViewById(R.id.button12); b13=(Button)findViewById(R.id.button13); b14=(Button)findViewById(R.id.button14); b2.setOnTouchListener(ontouch); b3.setOnTouchListener(ontouch); b4.setOnTouchListener(ontouch); b5.setOnTouchListener(ontouch); b6.setOnTouchListener(ontouch); b11.setOnTouchListener(ontouch); b12.setOnTouchListener(ontouch); b13.setOnTouchListener(ontouch); b14.setOnTouchListener(ontouch); } Button.OnTouchListener ontouch = new Button.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN://按下後執行 v.setBackgroundResource(R.drawable.b4);//換背景 switch(v.getId()) { case R.id.button11: setContentView(R.layout.activity_t1); break; case R.id.button12: break; } break; case MotionEvent.ACTION_UP://彈起後執行 switch(v.getId()) { case R.id.button2: case R.id.button3: case R.id.button4: v.setBackgroundResource(R.drawable.b1);//背景變回來 break; case R.id.button12: case R.id.button5: v.setBackgroundResource(R.drawable.b2);//背景變回來 break; case R.id.button6: case R.id.button11: case R.id.button14: v.setBackgroundResource(R.drawable.b3);//背景變回來 break; } break; } return false; } }; }
沒有留言:
張貼留言
影片的問題請留在影片的留言區裡。
部落格不會另外通知給我,所以很難發現你有留言。