ตัวอย่างโปรแกรมคำนวนค่า GDC หรือ ห.ร.ม.
XML ในส่วนของ User Interface
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android:id="@+id/widget39" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" > <TextView android:layout_height="wrap_content" android:text="Number 1 : " android:layout_width="wrap_content" android:id="@+id/LabelNumber1" android:layout_x="6dp" android:layout_y="24dp"></TextView> <TextView android:layout_height="wrap_content" android:text="Number 2 : " android:layout_width="wrap_content" android:id="@+id/LabelNumber2" android:layout_x="6dp" android:layout_y="82dp"></TextView> <EditText android:textSize="18sp" android:layout_height="wrap_content" android:layout_width="213px" android:id="@+id/EtNumber1" android:layout_x="80dp" android:layout_y="10dp"> <requestFocus></requestFocus> </EditText> <EditText android:textSize="18sp" android:layout_height="wrap_content" android:layout_width="214px" android:id="@+id/EtNumver2" android:layout_x="80dp" android:layout_y="66dp"></EditText> <TextView android:layout_height="wrap_content" android:text="GCD : " android:layout_width="wrap_content" android:id="@+id/ansTxt" android:layout_x="9dp" android:layout_y="139dp"></TextView> <TextView android:layout_height="wrap_content" android:text="Count Loop : " android:layout_width="wrap_content" android:id="@+id/countTxt" android:layout_x="122dp" android:layout_y="140dp"></TextView> <Button android:text="Calculate" android:id="@+id/BtnCalc" android:layout_height="wrap_content" android:layout_width="300px" android:layout_x="14dp" android:layout_y="183dp"></Button> <Button android:text="Reset" android:id="@+id/BtnReset" android:layout_height="wrap_content" android:layout_width="300px" android:layout_x="14dp" android:layout_y="240dp"></Button> </AbsoluteLayout>
Source Code ในส่วนของตัวโปรแกรม
package slayer.euclid; 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 EuclidActivity extends Activity { private EditText txtNumber1; private EditText txtNumber2; private TextView ansTxt; private TextView countTxt; private Button btnCalc; private Button BtnCalc; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initControls(); } private void initControls() { txtNumber1 = (EditText)findViewById(R.id.EtNumber1); txtNumber2 = (EditText)findViewById(R.id.EtNumver2); btnCalc = (Button)findViewById(R.id.BtnCalc); ansTxt = (TextView)findViewById(R.id.ansTxt); countTxt = (TextView)findViewById(R.id.countTxt); btnCalc.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ calculate(); }}); BtnCalc = (Button)findViewById(R.id.BtnReset); BtnCalc.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {reset();}}); } private void calculate() { long r,m,n; int i=0; m = Long.parseLong(txtNumber1.getText().toString()); n = Long.parseLong(txtNumber2.getText().toString()); while(n != 0) { r = m%n; m = n; n = r; i++; } ansTxt.setText("GCD : " + Long.toString(m)); countTxt.setText("Count Loop : " + Integer.toString(i)); } private void reset() { txtNumber1.setText(""); txtNumber2.setText(""); ansTxt.setText("GCD : "); countTxt.setText("Count Loop : "); } }
ไม่มีความคิดเห็น:
แสดงความคิดเห็น