Saturday, 12 September 2015

Source code cara membuat tombol Exit di Android



/*Code java :
package com.example.membuattbexit;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;

public class ExitActivity extends ActionBarActivity implements TextWatcher {
      private Button keluar;
      TextView seleksi;
      AutoCompleteTextView edit;
      String[] item = { "Januari", "February", "March", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober","November", "Dsember"};
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_exit);
           
            seleksi = (TextView) findViewById(R.id.seleksi);
            edit = (AutoCompleteTextView) findViewById(R.id.edit);
            edit.addTextChangedListener(this);
            edit.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, item));
            keluar = (Button) this.findViewById(R.id.keluar);
            keluar.setOnClickListener(new OnClickListener() {
                  @Override public void onClick(View arg0) {
                        ExitActivity.this.finish(); }
                  });
            }
      public void onTextChanged(CharSequence s, int start, int before, int count) {
      seleksi.setText(edit.getText());
      }
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // not used
            }
      public void afterTextChanged(Editable s)
      {
            // not used } }
      }

      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.exit, menu);
            return true;
      }

      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                  return true;
            }
            return super.onOptionsItemSelected(item);
      }
}


 /*Xml Code*/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<TextView
    android:id="@+id/seleksi"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
 <AutoCompleteTextView
     android:id="@+id/edit"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:completionThreshold="3"/>
 <Button
     android:layout_height="wrap_content"
     android:text="Keluar"
     android:layout_width="wrap_content"
     android:id="@+id/keluar"
     android:layout_gravity="right"/>
 </LinearLayout>


0 comments:

Post a Comment