08 January 2017

Android: Sự kiện button OnClickListener (bấm) và OnLongClickListener (bấm giữ)

activity_main.xml
Java Android 2017
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_marginTop="20dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >


    <EditText
        android:layout_weight="1"
        android:id="@+id/edt_number1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Number 1"
        android:paddingLeft="10dp" />

    <EditText
        android:layout_weight="1"
        android:id="@+id/edt_number2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Number 2"
        android:paddingLeft="10dp" />

    </LinearLayout>

    <Button
        android:text="Result"
        android:layout_marginTop="20dp"
        android:id="@+id/btn_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|bottom"
        />

</LinearLayout>
OnClickListener
Lắng nghe button cách 1 
package com.example.lonely.demolisttenner;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText edtNum1;
    private EditText edtNum2;
    private Button btnResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edtNum1 = (EditText) findViewById(R.id.edt_number1);
        edtNum2 = (EditText) findViewById(R.id.edt_number2);

        btnResult = (Button) findViewById(R.id.btn_result);
        btnResult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String result = edtNum1.getText() + " vs " + edtNum2.getText();

                Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
Lắng nghe button cách 2 (Thường sử dụng):
package com.example.lonely.demolisttenner;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText edtNum1;
    private EditText edtNum2;
    private Button btnResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edtNum1 = (EditText) findViewById(R.id.edt_number1);
        edtNum2 = (EditText) findViewById(R.id.edt_number2);

        btnResult = (Button) findViewById(R.id.btn_result);
        btnResult.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_result:
                String result = edtNum1.getText() + " vs " + edtNum2.getText();
                Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }
}
OnLongClickListener
package com.example.lonely.demolisttenner;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText edtNum1;
    private EditText edtNum2;
    private Button btnResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edtNum1 = (EditText) findViewById(R.id.edt_number1);
        edtNum2 = (EditText) findViewById(R.id.edt_number2);

        btnResult = (Button) findViewById(R.id.btn_result);
        btnResult.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                String result = edtNum1.getText() + " vs " + edtNum2.getText();

                Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }
}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang