Giới thiệu đến các bạn bài viết nằm trong lập trình android căn bản. Bài viết này sẽ giúp các bạn viết hàm tính tổng các số nguyên trên Android.
Đầu tiên mình tạo 1 TextView chứa nội dung “Nhập vào số nguyên dương N” và sửa Id của nó lại thành tV1.
Tiếp theo bạn tạo 1 EditText và sửa Id lại thành edt1.
Sau đó bạn tạo 1 Button Thực hiện và sửa Id lại thành btn1.
Cuối cùng bạn tạo 2 TextView là tV_day và tV_tong để nhận kết quả trả về.
Đoạn code file activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/tV1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="16dp" android:layout_marginTop="32dp" android:text="Nhập vào số nguyên dương N" /> <EditText android:id="@+id/edt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tV1" android:layout_below="@+id/tV1" android:layout_marginTop="20dp" android:ems="10" android:inputType="number" > <requestFocus /> </EditText> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/tV1" android:layout_below="@+id/edt1" android:layout_marginRight="49dp" android:layout_marginTop="26dp" android:text="Thực hiện" /> <TextView android:id="@+id/tV_day" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/btn1" android:layout_centerVertical="true" android:text="S=" /> <TextView android:id="@+id/tV_tong" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tV_day" android:layout_below="@+id/tV_day" android:layout_marginTop="16dp" android:text="S=" /> </RelativeLayout>
Tiếp đến, chúng ta thực hiện code bên file MainActivity.java:
Trong hàm onCreate ta khai báo:
<span style="font-size: large;">edt1=(EditText)findViewById(R.id.edt1); btn1=(Button)findViewById(R.id.btn1); tV_day=(TextView)findViewById(R.id.tV_day); tV_tong=(TextView)findViewById(R.id.tV_tong);</span>
Ta tiếp tục bắt sự kiện click cho Button:
<span style="font-size: large;">btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub }</span>
Trong sự kiện onClick dùng vòng lặp for chạy từ 1 đến N, mỗi bước cộng dồn giá trị vào biến tong, ghép thêm chuoi:
<span style="font-size: large;">int N=Integer.parseInt(edt1.getText().toString()); int tong=0; String chuoi=""; for(int i=1;i<=N;i++) { tong+=i; chuoi+=String.valueOf(i); chuoi+=(i!=N)?"+":""; } tV_day.setText("S= "+chuoi); tV_tong.setText("S= "+tong);</span>
Đến đây ta bắt đầu thực thi code của chúng ta, và kết quả được như sau:
POD_1
Hẹn gặp lại các bạn ở bài tiếp theo. Chúc các bạn thành công!