Button Click

În tutorialele anterioare am utilizat în nenumărate rânduri butoane,  unul dintre cel mai des întâlnite controale ale une interfeţe grafice.  Acest tutorial va acorda o atenţie deosebită tratării evenimentului de click ce se poate realiza asupra unui buton.

Există două modalităţi de a implementa tratarea evenimentului de click pentru un buton:

    1. în cadrul metodei onCreate;
button.setOnClickListener(new OnClickListener() { })
    1. în cadrul unei metode separate.
public void button_onClick(View view) { }

1. În cadrul metodei onCreate:

-layout xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
android:gravity="center_vertical|center_horizontal">
<Button android:id="@+id/btnStart"
android:layout_height="100dip"
android:layout_width="100dip"
android:text="Start">
</Button>
</LinearLayout>

-cod java:

package org.example.apps;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class Test extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnStart=(Button)findViewById(R.id.btnStart);
btnStart.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),
"Ati apasat butonul!", Toast.LENGTH_SHORT).show();
}
});
}
}

 

 

 

 

Afişare interfaţă grafică :

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Click button:

 




2. Metodă separată:

-layout xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
android:gravity="center_vertical|center_horizontal">
<Button android:id="@+id/btnStart"
android:layout_height="100dip"
android:layout_width="100dip"
android:text="Start"
android:onClick="btnStart_onClick">
</Button>
</LinearLayout>

-cod java:

package org.example.apps;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class Test extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void btnStart_onClick(View view) {
Toast.makeText(getApplicationContext(),
"Ati apasat butonul (metoda separata)!", Toast.LENGTH_SHORT).show();
}
}

Un alt punct de interes pentru personalizarea unui buton este reprezentat de definirea unui fişier xml de tip style. Acesta presupune şi existenşa unor resurse (imagini, culori):

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="gray">#B9C2C9</drawable>
<drawable name="red">#FF0000</drawable>
<drawable name="white">#FFFFFF</drawable>
</resources>

style_btn_start.xml (va fi adăugat în drawable)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item  android:state_enabled="false"
android:drawable="@drawable/gray" />
<item android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/red" />
<item android:state_focused="true"
android:state_enabled="true" android:drawable="@drawable/red" />
<item  android:state_enabled="true"
android:drawable="@drawable/gray" />
</selector>

De menţionat că în tag-ul de declarare a butonului se mai adaugă:

android:background="@drawable/style_btn_start"

Rezultatul vizual:

Advertisements
Button Click

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s