By GokiSoft.com| 20:12 20/11/2020|
Android

[Share Code] Tạo dự án android đầu tay - Hello World - Lập trình Android


#activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.gokisoft.c1812l.MainActivity">

    <TextView
        android:id="@+id/txt_hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/button_test" />

</LinearLayout>


#strings.xml


<resources>
    <string name="app_name">C1812L</string>
    <string name="hello">Hello World!!!</string>
    <string name="button_test">Click Test</string>
</resources>


#MainActivity.java


package com.gokisoft.c1812l;

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

public class MainActivity extends AppCompatActivity {
    TextView txtHello;
    Button btnTest;

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

        txtHello = findViewById(R.id.txt_hello);
        btnTest = findViewById(R.id.button_test);

        txtHello.setText("OKOK");

        btnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "TEST",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}


Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)