Senin, 02 Oktober 2017

Materi SetOnClickListener

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{
Button keluar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

keluar = (Button) findViewById(R.id.exitBtn1);
keluar.setOnClickListener(this);

Button linearLayout = (Button) findViewById(R.id.ButtonLinear);
linearLayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View linear) {
Intent myIntent = new Intent(linear.getContext(),
LinearActivity.class);
startActivityForResult(myIntent, 0);
}
});
Button relativeLayout = (Button) findViewById(R.id.ButtonRelative);
relativeLayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View relative) {
Intent myIntent = new Intent(relative.getContext(),
RelativeActivity.class);
startActivityForResult(myIntent, 0);
}
});
Button tableLayout = (Button) findViewById(R.id.ButtonTable);
tableLayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View table) {
Intent myIntent = new Intent(table.getContext(),
TableActivity.class);
startActivityForResult(myIntent, 0);
}
});

}

public void onClick(View clicked) { 
        switch (clicked.getId()) {   
            case R.id.exitBtn1: exit(); break; 
        } 
    }

private void exit() { 
        AlertDialog.Builder builder = new AlertDialog.Builder(this); 
        builder.setMessage("Apakah Kamu Benar-Benar ingin keluar?") 
        .setCancelable(false) 
        .setPositiveButton("Ya", new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int id) { 
                MainActivity.this.finish(); 
                } 
            }) 
            .setNegativeButton("Tidak",new DialogInterface.OnClickListener() { 
                @Override 
                public void onClick(DialogInterface dialog, int arg1) {                     
                    dialog.cancel(); 
        }    
        }).show(); 
    } 
    

}

Tidak ada komentar:

Posting Komentar