歡迎訂閱杜仲教學頻道: https://www.youtube.com/channel/UCElwt6s8Ea_V-gnu9sZZT5g?sub_confirmation=1 杜仲教學部落格: https://derekcctu.blogspot.com/
2018年12月21日 星期五
[Android]_ch1.5_Button元件共用監聽器
[Android]_ch1.5_Button元件共用監聽器
package com.gjun.ch1_buttonclick;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Act01 extends Activity {
TextView tv1;
Button btn1,btn2,btn3,btn4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1=(TextView)findViewById(R.id.textView1);
btn1=(Button)findViewById(R.id.button1);
btn2=(Button)findViewById(R.id.button2);
btn3=(Button)findViewById(R.id.button3);
btn4=(Button)findViewById(R.id.button4);
OnClickListener OCL=new OnClickListener(){
@Override
public void onClick(View v) {
int id=((Button)v).getId();
String name="";
switch(id){
case R.id.button1:
name=btn1.getText().toString();
tv1.setText(name);
tv1.setBackgroundColor(Color.RED);
tv1.setTextSize(16);
break;
case R.id.button2:
name=btn2.getText().toString();
tv1.setText(name);
tv1.setBackgroundColor(Color.GREEN);
tv1.setTextSize(26);
break;
case R.id.button3:
Resources r=getResources();
name=r.getString(R.string.b3);
tv1.setText(name);
tv1.setBackgroundColor(Color.BLUE);
tv1.setTextSize(36);
break;
case R.id.button4:
tv1.setText("離開");
finish();
break;
}
}};
btn1.setOnClickListener(OCL);
btn2.setOnClickListener(OCL);
btn3.setOnClickListener(OCL);
btn4.setOnClickListener(OCL);
}
}
訂閱:
張貼留言 (Atom)
Ether.fi Cash Card 一張所有人必備的加密貨幣信用卡 [Ether.fi Cash Card] : https://www.ether.fi/app/cash/sign-up?ref_code=d27f8523 ------國際交易所------ [Bitg...
-
22.Hamster Kombat Daily Combo倉鼠戰鬥_DailyCombo0620 ------國際交易所------ [Bitget 交易所]: https://partner.bitget.com/bg/DCA888 邀請碼:DCA888 [Bybit交...
-
[CATS coin每日任務(Ton區塊鏈游戲伏擊百倍幣)] https://t.me/catsgang_bot/join?startapp=FXvTt0OIdo7NJ4jxDutEV ------國際交易所------ [Bitget 交易所]: https://partn...
沒有留言:
張貼留言