尝试使用适用于Android的GCF应用

时间:2010-06-15 00:10:13

标签: java android

我是Android新手,我正在尝试创建一个非常基本的应用程序,用于计算和显示用户输入的两个数字的GCF。这是我的GCF.java的副本:

package com.example.GCF;

import java.util.Arrays;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class GCF extends Activity {
private TextView mAnswer;
    private EditText mA, mB;
    private Button ok;
    private String A, B;
    private int iA, iB;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mA = (EditText) findViewById(R.id.entry);
        mB = (EditText) findViewById(R.id.entry1);
        ok = (Button) findViewById(R.id.ok);
        mAnswer = (TextView) findViewById(R.id.answer1);

        ok.setOnClickListener(new OnClickListener() { 
        public void onClick(View v)  
        {               
            A = mA.getText().toString();  
            B = mB.getText().toString();   
        } 
        });

        // the String to int conversion happens here
        iA = Integer.parseInt(A.trim());
        iB = Integer.parseInt(B.trim());

        while (iA != iB) {
        int[] nums={ iA, iB, Math.abs(iA-iB) };
        Arrays.sort(nums);
            iA=nums[0];
        iB=nums[1]; 
        } 
        updateDisplay();
    }
        private void updateDisplay() {
          mAnswer.setText(
            new StringBuilder().append(iA));

        }

}

有什么建议吗?谢谢!

1 个答案:

答案 0 :(得分:0)

你可能想要这个(对于你的onCreate函数)

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mA = (EditText) findViewById(R.id.entry);
    mB = (EditText) findViewById(R.id.entry1);
    ok = (Button) findViewById(R.id.ok);
    mAnswer = (TextView) findViewById(R.id.answer1);

    ok.setOnClickListener(new OnClickListener() { 
        public void onClick(View v) {               
            A = mA.getText().toString();  
            B = mB.getText().toString();

            // the String to int conversion happens here
            iA = Integer.parseInt(A.trim());
            iB = Integer.parseInt(B.trim());

            while (iA != iB) {
                int[] nums={ iA, iB, Math.abs(iA-iB) };
                Arrays.sort(nums);
                iA=nums[0];
                iB=nums[1]; 
                } 
                updateDisplay();
        }
    });
}