Baru_Berbagi

MainActivity.java

May 29th, 2021
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. package com.baruberbagi.converter;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.TextView;
  10. import android.widget.Toast;
  11.  
  12. public class MainActivity extends AppCompatActivity {
  13.  
  14.     EditText edtNomor;
  15.     Button btnKonversi;
  16.     TextView txtBin, txtOct,txtHex;
  17.  
  18.     @Override
  19.     protected void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.activity_main);
  22.  
  23.         edtNomor = findViewById(R.id.edtNomor);
  24.         btnKonversi = findViewById(R.id.btnKonversi);
  25.         txtBin = findViewById(R.id.txtBin);
  26.         txtOct = findViewById(R.id.txtOct);
  27.         txtHex = findViewById(R.id.txtHex);
  28.  
  29.         btnKonversi.setOnClickListener(new View.OnClickListener() {
  30.             @Override
  31.             public void onClick(View v) {
  32.                 try {
  33.  
  34.                         int dec = Integer.parseInt(edtNomor.getText().toString());
  35.  
  36.                         String bin = Integer.toBinaryString(dec);
  37.                         String oct = Integer.toOctalString(dec);
  38.                         String hex = Integer.toHexString(dec);
  39.  
  40.                         txtBin.setText(bin);
  41.                         txtOct.setText(oct);
  42.                         txtHex.setText(hex);
  43.  
  44.  
  45.                 }catch (Exception e){
  46.                     Toast.makeText(getApplicationContext(),"Error! Angka melebihi batas / mungkin belum di isi",Toast.LENGTH_SHORT).show();
  47.                 }
  48.  
  49.  
  50.             }
  51.         });
  52.  
  53.     }
  54. }
  55.  
Add Comment
Please, Sign In to add comment