簡體   English   中英

在編輯文本中獲取兩個或多個值,然后單擊按鈕獲取結果

[英]get two or more values in an edit text and get the result from a button click

我正在使用Android Studio構建GPA計算器

我有的:

我有一個信用單元的編輯文本,另一個有不同ID的等級文本

我有一個按鈕的方法

該應用程序運行良好,但是當我輸入一個學分的值和一個分數的值時,單擊按鈕不會發生任何反應。

這是我的xml

編輯信用單位的文本

<EditText
        android:id="@+id/txt_credits"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dp"
        android:layout_weight="1"
        android:background="@drawable/border"
        android:ems="4"
        android:gravity="center"
        android:hint="e.g. 0, 2, 3 "
        android:inputType="textPersonName|number|numberDecimal" />

編輯成績文字

        EditText
        android:id="@+id/txt_grade"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dp"
        android:layout_weight="1"
        android:background="@drawable/border"
        android:ems="4"
        android:gravity="center"
        android:hint="e.g. A, B, C "
        android:inputType="textCapCharacters"
        android:paddingLeft="16dp" />

紐扣

<Button
            android:id="@+id/button2"
            android:layout_marginBottom="6dp"
            android:textAllCaps="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="32dp"
            android:onClick="calcGpa"
            android:text="get gpa"
           android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"/>

這是我的java

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mLayout = (LinearLayout) findViewById(R.id.linearLayout);
        mEditCreditText = (EditText) findViewById(R.id.txt_credits);
        mEditGradeText =  (EditText) findViewById(R.id.txt_grade);
        mButton = (Button) findViewById(R.id.button2);
        mButton.setOnClickListener(onClick());

    }
    private View.OnClickListener onClick() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {



                allCredits.add(mEditCreditText);
                allGrades.add(mEditGradeText);
            }
        };
    }




    public int pointPerSubj(int number,String grade) {
        int point;

        if (grade.equalsIgnoreCase("A"))
            point = 5;
        else if (grade.equalsIgnoreCase("B"))
            point = 4;
        else if (grade.equalsIgnoreCase("C"))
            point = 3;
        else if (grade.equalsIgnoreCase("D"))
            point = 2;
        else
            point = 0;

        int perSubjectPoint = number * point;
        return perSubjectPoint;
    }

    public void calcGpa (View v) {

         gpa = perSubjectPoint/totalCredits;

        display(gpa);
        }


    // display method
    private void display(int number) {
        TextView gpaTextView = (TextView) findViewById(R.id.gpa_text_view);
        gpaTextView.setText("" + (number));
    }

    }

我沒有足夠的聲譽來發表評論,否則我會在評論中要求提供更多代碼。

您的onClickListener將您的EditTexts添加到我假設的EditText的ArrayLists中: allCreditsallGrades

首先,如果要插入文本,則這些文本應該是字符串的ArrayLists,並且應該這樣做:

allCredits.add(mEditCreditText.getText().toString());
allGrades.add(mEditGradeText.getText().toString());

另外,您的代碼沒有指出在何處調用calcGpapointPerSubj方法。

我相信您只是沒有在調用所需的方法來顯示任何內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM