標題:
多按鈕共用事件
[打印本頁]
作者:
tonyh
時間:
2017-5-6 14:54
標題:
多按鈕共用事件
本帖最後由 tonyh 於 2017-5-13 17:52 編輯
[attach]2204[/attach]
package com.example.student.myapplication;
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 {
private TextView tv_show;
private Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12,btn13;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_show= (TextView) findViewById(R.id.textView2);
btn1= (Button) findViewById(R.id.button1);
btn2= (Button) findViewById(R.id.button2);
btn3= (Button) findViewById(R.id.button3);
btn4= (Button) findViewById(R.id.button4);
btn5= (Button) findViewById(R.id.button5);
btn6= (Button) findViewById(R.id.button6);
btn7= (Button) findViewById(R.id.button7);
btn8= (Button) findViewById(R.id.button8);
btn9= (Button) findViewById(R.id.button9);
btn10= (Button) findViewById(R.id.button10);
btn11= (Button) findViewById(R.id.button11);
btn12= (Button) findViewById(R.id.button12);
btn13= (Button) findViewById(R.id.button13);
btn1.setOnClickListener(myListener);
btn2.setOnClickListener(myListener);
btn3.setOnClickListener(myListener);
btn4.setOnClickListener(myListener);
btn5.setOnClickListener(myListener);
btn6.setOnClickListener(myListener);
btn7.setOnClickListener(myListener);
btn8.setOnClickListener(myListener);
btn9.setOnClickListener(myListener);
btn10.setOnClickListener(myListener);
btn11.setOnClickListener(myListener);
btn12.setOnClickListener(myListener);
btn13.setOnClickListener(myListener);
}
private View.OnClickListener myListener=new View.OnClickListener() {
@Override
public void onClick(View v) {
String s=tv_show.getText().toString();
switch (v.getId())
{
case R.id.button1:
tv_show.setText(s+"1");
break;
case R.id.button2:
tv_show.setText(s+"2");
break;
case R.id.button3:
tv_show.setText(s+"3");
break;
case R.id.button4:
tv_show.setText(s+"4");
break;
case R.id.button5:
tv_show.setText(s+"5");
break;
case R.id.button6:
tv_show.setText(s+"6");
break;
case R.id.button7:
tv_show.setText(s+"7");
break;
case R.id.button8:
tv_show.setText(s+"8");
break;
case R.id.button9:
tv_show.setText(s+"9");
break;
case R.id.button10:
tv_show.setText(s+"*");
break;
case R.id.button11:
tv_show.setText(s+"0");
break;
case R.id.button12:
tv_show.setText(s+"#");
break;
case R.id.button13:
if(s.length()>=1)
tv_show.setText(s.substring(0,s.length()-1));
break;
}
}
};
}
複製代碼
作者:
劉得恩
時間:
2017-5-13 17:24
[attach]2222[/attach]
作者:
張彥承
時間:
2017-5-13 17:58
package com.example.student.myapplication;
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 {
private TextView tv_show;
private Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_show=(TextView)findViewById(R.id.tv2);
btn1=(Button)findViewById((R.id.button1);
btn2=(Button)findViewById((R.id.button2);
btn3=(Button)findViewById((R.id.button3);
btn4=(Button)findViewById((R.id.button4);
btn5=(Button)findViewById((R.id.button5);
btn6=(Button)findViewById((R.id.button6);
btn7=(Button)findViewById((R.id.button7);
btn8=(Button)findViewById((R.id.button8);
btn9=(Button)findViewById((R.id.button9);
btn10=(Button)findViewById((R.id.button10);
btn11=(Button)findViewById((R.id.button11);
btn11=(Button)findViewById((R.id.button12);
btn1.setOnClickListener(myListener);
btn2.setOnClickListener(myListener);
btn3.setOnClickListener(myListener);
btn4.setOnClickListener(myListener);
btn5.setOnClickListener(myListener);
btn6.setOnClickListener(myListener);
btn7.setOnClickListener(myListener);
btn8.setOnClickListener(myListener);
btn9.setOnClickListener(myListener);
btn10.setOnClickListener(myListener);
btn11.setOnClickListener(myListener);
}
private View.OnClickListener myListener=new View.OnClickListener(){
@Override
public void onClick(View v)
{
String s=tv_show.getText().toString():
switch(v.getId()) {
case R.id.button1:
tv_show.setText(s + "1");
break;
case R.id.button2:
tv_show.setText(s + "2");
break;
case R.id.button3:
tv_show.setText(s + "3");
break;
case R.id.button4:
tv_show.setText(s + "4");
break;
case R.id.button5:
tv_show.setText(s + "5");
break;
case R.id.button6:
tv_show.setText(s + "6");
break;
case R.id.button7:
tv_show.setText(s + "7");
break;
case R.id.button8:
tv_show.setText(s + "8");
break;
case R.id.button9:
tv_show.setText(s + "9");
break;
case R.id.button10:
tv_show.setText(s + "*");
break;
case R.id.button11:
tv_show.setText(s + "0");
break;
case R.id.button12:
tv_show.setText(s + "#");
break;
}
}
}:
}
複製代碼
作者:
林宇翔
時間:
2017-5-13 18:02
package com.example.student.myapplication;
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 {
private Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv= (TextView) findViewById(R.id.textView2);
btn1= (Button) findViewById(R.id.button);
btn2= (Button) findViewById(R.id.button2);
btn3= (Button) findViewById(R.id.button3);
btn4= (Button) findViewById(R.id.button4);
btn5= (Button) findViewById(R.id.button5);
btn6= (Button) findViewById(R.id.button6);
btn7= (Button) findViewById(R.id.button7);
btn8= (Button) findViewById(R.id.button8);
btn9= (Button) findViewById(R.id.button9);
btn10= (Button) findViewById(R.id.button10);
btn11= (Button) findViewById(R.id.button11);
btn12= (Button) findViewById(R.id.button12);
btn1.setOnClickListener(myListener);
btn2.setOnClickListener(myListener);
btn3.setOnClickListener(myListener);
btn4.setOnClickListener(myListener);
btn5.setOnClickListener(myListener);
btn6.setOnClickListener(myListener);
btn7.setOnClickListener(myListener);
btn8.setOnClickListener(myListener);
btn9.setOnClickListener(myListener);
btn10.setOnClickListener(myListener);
btn11.setOnClickListener(myListener);
btn12.setOnClickListener(myListener);
}
private View.OnClickListener myListener=new View.OnClickListener() {
@Override
public void onClick(View v) {
String s=tv.getText().toString();
switch (v.getId())
{
case R.id.button:
tv.setText(s+"1");
break;
case R.id.button2:
tv.setText(s+"2");
break;
case R.id.button3:
tv.setText(s+"3");
break;
case R.id.button4:
tv.setText(s+"4");
break;
case R.id.button5:
tv.setText(s+"5");
break;
case R.id.button6:
tv.setText(s+"6");
break;
case R.id.button7:
tv.setText(s+"7");
break;
case R.id.button8:
tv.setText(s+"8");
break;
case R.id.button9:
tv.setText(s+"9");
break;
case R.id.button10:
tv.setText(s+"*");
break;
case R.id.button11:
tv.setText(s+"0");
break;
case R.id.button12:
tv.setText(s+"#");
break;
}
}
};
}
複製代碼
作者:
李允軒
時間:
2017-5-14 08:29
package com.example.jerry.myapplication;
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 {
private Button bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,bt10,bt11,bt12;
private TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1= (Button) findViewById(R.id.button);
bt2= (Button) findViewById(R.id.button2);
bt3= (Button) findViewById(R.id.button3);
bt4= (Button) findViewById(R.id.button4);
bt5= (Button) findViewById(R.id.button5);
bt6= (Button) findViewById(R.id.button6);
bt7= (Button) findViewById(R.id.button7);
bt8= (Button) findViewById(R.id.button8);
bt9= (Button) findViewById(R.id.button9);
bt10= (Button) findViewById(R.id.button10);
bt11= (Button) findViewById(R.id.button11);
bt12= (Button) findViewById(R.id.button12);
tv1= (TextView) findViewById(R.id.textView2);
bt1.setOnClickListener(myListener);
bt2.setOnClickListener(myListener);
bt3.setOnClickListener(myListener);
bt4.setOnClickListener(myListener);
bt5.setOnClickListener(myListener);
bt6.setOnClickListener(myListener);
bt7.setOnClickListener(myListener);
bt8.setOnClickListener(myListener);
bt9.setOnClickListener(myListener);
bt10.setOnClickListener(myListener);
bt11.setOnClickListener(myListener);
bt12.setOnClickListener(myListener);
tv1.setOnClickListener(myListener);
}
private View.OnClickListener myListener=new View.OnClickListener(){
@Override
public void onClick(View v) {
String s=tv1.getText().toString();
switch (v.getId()){
case R.id.button:
tv1.setText(s+"1");
break;
case R.id.button2:
tv1.setText(s+"2");
break;
case R.id.button3:
tv1.setText(s+"3");
break;
case R.id.button4:
tv1.setText(s+"4");
break;
case R.id.button5:
tv1.setText(s+"5");
break;
case R.id.button6:
tv1.setText(s+"6");
break;
case R.id.button7:
tv1.setText(s+"7");
break;
case R.id.button8:
tv1.setText(s+"8");
break;
case R.id.button9:
tv1.setText(s+"9");
break;
case R.id.button10:
tv1.setText(s+"*");
break;
case R.id.button11:
tv1.setText(s+"0");
break;
case R.id.button12:
tv1.setText(s+"#");
break;
}
}
};
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2