本帖最後由 劉得恩 於 2018-5-23 22:09 編輯
- package com.example.adm.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;
- private Button btn[]=new Button[13];
- private int ids[]=new int[]{R.id.btn0,R.id.btn1,R.id.btn2,R.id.btn3,R.id.btn4,R.id.btn5,R.id.btn6,R.id.btn7,R.id.btn8,R.id.btn9,R.id.btn10,R.id.btn11,R.id.btn12};
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- tv=(TextView)findViewById(R.id.tv);
- for(int i=0;i<13;i++) {
- btn[i] = (Button) findViewById(ids[i]);
- btn[i].setOnClickListener(MyListener);
- }
- btn[12].setOnLongClickListener(new View.OnLongClickListener() {
- @Override
- public boolean onLongClick(View v) {
- tv.setText("");
- return true;
- }
- });
- }
- private View.OnClickListener MyListener =new View.OnClickListener(){
- @Override
- public void onClick(View v)
- {
- String str =tv.getText().toString();
- int i=0,id=v.getId();
- for(;i<13;i++)
- if(ids[i]==id)
- break;
- if(i<10)
- tv.setText(str+Integer.toString(i));
- else if(i==10)
- tv.setText(str+"*");
- else if(i==11)
- tv.setText(str+"#");
- else if(i==12&&str.length()>0)
- tv.setText(str.substring(0,str.length()-1));
- }
- };
- }
複製代碼 |