private View.OnClickListener myLinstener= new View.OnClickListener() {
@Override
public void onClick(View v) {
String str=tv1.getText().toString();
switch (v.getId()){
case R.id.button1:
tv1.setText(str+"1");
break;
case R.id.button2:
tv1.setText(str+"2");
break;
case R.id.button3:
tv1.setText(str+"3");
break;
case R.id.button4:
tv1.setText(str+"4");
break;
case R.id.button5:
tv1.setText(str+"5");
break;
case R.id.button6:
tv1.setText(str+"6");
break;
case R.id.button7:
tv1.setText(str+"7");
break;
case R.id.button8:
tv1.setText(str+"8");
break;
case R.id.button9:
tv1.setText(str+"9");
break;
case R.id.button10:
tv1.setText(str+"*");
break;
case R.id.button11:
tv1.setText(str+"0");
break;
case R.id.button12:
tv1.setText(str+"#");
break;
case R.id.button13:
if(str.length()>0)
str=str.substring(0,str.length()-1);
tv1.setText(str);
}
}
};
private View.OnLongClickListener myListener2= new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if(v.getId()==R.id.button13)
tv1.setText("");
return true;
}
};
}
複製代碼
作者: 劉得恩 時間: 2018-5-23 18:09
本帖最後由 劉得恩 於 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};