android进制计算器
时间: 2023-06-30 17:08:16 浏览: 150
以下是一个简单的Android进制计算器的代码实现,可以进行二进制、八进制、十进制和十六进制的转换和计算:
```java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText etDec, etHex, etOct, etBin;
Button btnDec, btnHex, btnOct, btnBin, btnCls, btnDel;
TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etDec = findViewById(R.id.etDec);
etHex = findViewById(R.id.etHex);
etOct = findViewById(R.id.etOct);
etBin = findViewById(R.id.etBin);
btnDec = findViewById(R.id.btnDec);
btnHex = findViewById(R.id.btnHex);
btnOct = findViewById(R.id.btnOct);
btnBin = findViewById(R.id.btnBin);
btnCls = findViewById(R.id.btnCls);
btnDel = findViewById(R.id.btnDel);
tvResult = findViewById(R.id.tvResult);
// 十进制转换
btnDec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String decStr = etDec.getText().toString().trim();
if (!decStr.equals("")) {
int dec = Integer.parseInt(decStr);
etHex.setText(Integer.toHexString(dec));
etOct.setText(Integer.toOctalString(dec));
etBin.setText(Integer.toBinaryString(dec));
}
}
});
// 十六进制转换
btnHex.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String hexStr = etHex.getText().toString().trim();
if (!hexStr.equals("")) {
int dec = Integer.parseInt(hexStr, 16);
etDec.setText(String.valueOf(dec));
etOct.setText(Integer.toOctalString(dec));
etBin.setText(Integer.toBinaryString(dec));
}
}
});
// 八进制转换
btnOct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String octStr = etOct.getText().toString().trim();
if (!octStr.equals("")) {
int dec = Integer.parseInt(octStr, 8);
etDec.setText(String.valueOf(dec));
etHex.setText(Integer.toHexString(dec));
etBin.setText(Integer.toBinaryString(dec));
}
}
});
// 二进制转换
btnBin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String binStr = etBin.getText().toString().trim();
if (!binStr.equals("")) {
int dec = Integer.parseInt(binStr, 2);
etDec.setText(String.valueOf(dec));
etHex.setText(Integer.toHexString(dec));
etOct.setText(Integer.toOctalString(dec));
}
}
});
// 清空输入框
btnCls.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
etDec.setText("");
etHex.setText("");
etOct.setText("");
etBin.setText("");
tvResult.setText("");
}
});
// 删除最后一个字符
btnDel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String decStr = etDec.getText().toString().trim();
String hexStr = etHex.getText().toString().trim();
String octStr = etOct.getText().toString().trim();
String binStr = etBin.getText().toString().trim();
if (!decStr.equals("")) {
etDec.setText(decStr.substring(0, decStr.length() - 1));
}
if (!hexStr.equals("")) {
etHex.setText(hexStr.substring(0, hexStr.length() - 1));
}
if (!octStr.equals("")) {
etOct.setText(octStr.substring(0, octStr.length() - 1));
}
if (!binStr.equals("")) {
etBin.setText(binStr.substring(0, binStr.length() - 1));
}
}
});
// 计算结果
tvResult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String decStr = etDec.getText().toString().trim();
String hexStr = etHex.getText().toString().trim();
String octStr = etOct.getText().toString().trim();
String binStr = etBin.getText().toString().trim();
if (!decStr.equals("") && hexStr.equals("") && octStr.equals("") && binStr.equals("")) {
tvResult.setText(decStr);
} else if (decStr.equals("") && !hexStr.equals("") && octStr.equals("") && binStr.equals("")) {
tvResult.setText(hexStr);
} else if (decStr.equals("") && hexStr.equals("") && !octStr.equals("") && binStr.equals("")) {
tvResult.setText(octStr);
} else if (decStr.equals("") && hexStr.equals("") && octStr.equals("") && !binStr.equals("")) {
tvResult.setText(binStr);
} else {
tvResult.setText("请输入正确的数值");
}
}
});
}
}
```
此代码实现了四种进制之间的转换和计算,并且支持清空输入框和删除最后一个字符的功能。可以根据需要进一步完善。
阅读全文