kiem tien, kiem tien online, kiem tien truc tuyen, kiem tien tren mang
Thứ Tư, 5 tháng 3, 2014


Vi dụ: Xây dựng ứng dụng nhập vào số nguyên n; tính n!; in kết quả.

>> Thực hiện: 

- Create Android Project: TinhGiaiThua





- Thiết lập và code:

+ Res\layout\main.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:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    
       

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/n" />

        <EditText
            android:id="@+id/txtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.12" 
            android:hint="Input"
            android:inputType="text|textAutoCorrect|textCapWords|number"
            />

           <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.12"
            android:text="Tinh" />
        
    <TextView
        android:id="@+id/kq"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/kq" />
    
    

</LinearLayout>

+ Res\value\string.xml



<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Nhap so can tinh</string>
    <string name="app_name">Tính giai thừa</string>
    <string name="kq">Kết quả:</string><string name="n">N:</string>
    

</resources>

+ rsc\...

package txt.Begin.TinhGiaiThua;


import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;
public class TinhGiaiThuaActivity extends Activity {     // Phuong thuc tinh giai thua public int GiaiThua(int a){ int gt=1; for(int i=1;i<=a;i++) gt=gt*i; return gt; } // onCreate     public void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);        setContentView(R.layout.main);               final Button tinh=(Button) findViewById(R.id.button1);        final TextView ketqua=(TextView) findViewById(R.id.kq);                     // xu ly nut button        tinh.setOnClickListener(new Button.OnClickListener(){        public void onClick(View v) {        String so=((EditText) findViewById(R.id.txtn)).getText().toString();        int n=Integer.parseInt(so);        try
{        int s=GiaiThua(n);        ketqua.setText("");        ketqua.setText(ketqua.getText()+""+(s));         } catch (Exception ex)
{
ketqua.setText(ex.toString());
}        }                });                   }}


>> Kết quả:


05 Mar 2014

0 nhận xét:

Đăng nhận xét

:) :)) ;(( :-) =)) ;( ;-( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ $-) (b) (f) x-) (k) (h) (c) cheer
Click to see the code!
To insert emoticon you must added at least one space before the code.

domain, domain name, premium domain name for sales

Bài đăng phổ biến