本帖最後由 tonyh 於 2017-5-13 17:52 編輯
- 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;
- }
- }
- };
- }
複製代碼 |