Yêu cầu Demo02 này,
Ta thiết kế giao diện gồm 1 button, 1 textview hiển thị kết quả, 1 edittext cho người dùng nhập tên.
Khi nhập tên vào và nhấn button, kết quả xin chào bạn ABC.
Viết source cho file string.xml,
File này cho phép ta extract 1 chuỗi dùng cho các view trong layout.
<resources> <string name="app_name">Demo2</string> <string name="Name">Nhao ten vao day</string> <string name="Button">NutNhan</string> <string name="TextView">Xuatra</string> <string name="Goiy">vd: Hoa</string> </resources>
Source file 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=".MainActivity"> <EditText android:id="@+id/edit_ten" android:layout_width="wrap_content" android:layout_height="wrap_content" tools:ignore="Autofill" android:hint="@string/Goiy" android:layout_margin="50dp" android:inputType="textPersonName" android:text="@string/Name"/> <Button android:id="@+id/btn_nutnhan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="50dp" android:text="@string/Button"/> <TextView android:id="@+id/txt_hienthi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="60dp" android:text="@string/TextView" /> </LinearLayout>
package com.example.demo2; 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.TextView; public class MainActivity extends AppCompatActivity { private Button btn; private EditText ten; private TextView hienthi; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btn_nutnhan); ten = (EditText) findViewById(R.id.edit_ten); hienthi = (TextView) findViewById(R.id.txt_hienthi); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String s = ten.getText().toString(); hienthi.setText("Xin chao ban " +s); } }); }
Kết quả,
No comments:
Post a Comment