MainActivity.java (Kalkulator Sederhana)

package com.unsada.kalkulator; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Switch; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button btntambah,btnkurang,btnbagi,btnkali; private EditText etfirst,etsecond; private TextView hasil; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); init(); } private void init() { btntambah = (Button)findViewById(R.id.tambah); btnkurang = (Button)findViewById(R.id.kurang); btnbagi = (Button)findViewById(R.id.bagi); btnkali = (Button)findViewById(R.id.kali); etfirst = (EditText) findViewById(R.id.etfirstvalue); etsecond = (EditText) findViewById(R.id.etsecondvalue); hasil = (TextView)findViewById(R.id.hasil); btntambah.setOnClickListener(this); btnkurang.setOnClickListener(this); btnbagi.setOnClickListener(this); btnkali.setOnClickListener(this); } @Override public void onClick(View view) { String num1 = etfirst.getText().toString(); String num2 = etsecond.getText().toString(); switch(view.getId()){ case R.id.tambah: int hasiltambah = Integer.parseInt(num1) + Integer.parseInt(num2); hasil.setText(String.valueOf(hasiltambah)); break; case R.id.kurang: int hasilkurang = Integer.parseInt(num1) - Integer.parseInt(num2); hasil.setText(String.valueOf(hasilkurang)); break; case R.id.bagi: try{ int hasilbagi = Integer.parseInt(num1) / Integer.parseInt(num2); hasil.setText(String.valueOf(hasilbagi)); }catch (Exception e){ hasil.setText("Tidak bisa dibagi!!!!"); } break; case R.id.kali: int hasilkali = Integer.parseInt(num1) * Integer.parseInt(num2); hasil.setText(String.valueOf(hasilkali)); break; } } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.