0

我正在開發一個簡單的Android應用程序,當用戶輸入一個數字時(EditText框不允許其他字符從XML代碼),按下一個按鈕,並使用幾個數學步驟處理該數字,以在同一屏幕上將結果TextView輸出。到目前爲止這麼好 - 該應用程序的作品!但是,爲避免製作兩個Android應用程序並將其全部保存在一個屏幕/應用程序中,我希望添加一個類似的第二個用戶EditText輸入,以便給出稍微不同的答案(稍微改變那裏的數學,並且用戶知道這個),因此用戶可以選擇將數字添加到哪個輸入框。如果我複製大部分代碼(使用不同的對象:id名稱等),則顯然會發生錯誤。可能我需要使用if .... else或條件OR變量的條件語句來返回一個布爾值true/false並相應地進行處理。如果在一個輸入框中沒有收到輸入,那麼Java代碼會檢查(並且)處理另一個EditText框中的輸入或任何可以使用的輸入小部件。我很高興爲每個和兩個回答TextView小部件使用兩個輸入選項和一個按鈕(如果編碼更容易)或僅保留一個按鈕和一個回答TextView輸出。考慮只使用一個(JavaMainActivity)Java文件和必要的Android活動XML文件來合併這些添加,是否存在標準化的代碼?使用Java檢查用戶是否在Android應用程序的兩個EditText框中輸入了一些東西

爲了在屏幕上使用兩個RadioButton(如建議),Java代碼如下所示用於檢查。其他小部件的XML代碼在其他XML文件中可以使用。由於未解析的符號,編譯器會標記四個錯誤。

當在屏幕上單擊按鈕時,會調用以下方法。這只是MainActivity Java文件的一部分,用於演示要編輯/更改的主要功能代碼。我已經記得添加android.widget.RadioButton包。

public void add(View v) 
    { 

    //initialize objects 
    TextView result=(TextView)findViewById(R.id.result); 
    EditText edit1=(EditText)findViewById(R.id.editText1); 


    // get text from edit text boxes and convert into double 
    double a=Double.parseDouble(String.valueOf(edit1.getText())); 

    //do the maths (as an example) 

    double b = a + 1000; 
    double c = b/5; 


    // display maths output on TextView - I left this comment as this is where the 
    // result.setText(.....) is normally placed without the radio button choices. 


    // Alternative maths for second choice of result for the App user. 
    double a1=Double.parseDouble(String.valueOf(edit1.getText())); 

    double b1 = a1 * 1000; 
    double c1 = b1 * 5; 


    } 

    public void onRadioButtonClicked(View v) { 
    // Is the button now checked? 
    boolean checked = ((RadioButton) v).isChecked(); 

    // Check which radio button was clicked 
    switch(v.getId()) { 
    case R.id.small: 
    if (checked) 
    result.setText("Answer:"+c); 

    break; 
    case R.id.large: 
    if (checked) 
    result.setText("Answer:"+c1); 
    break; 
    } 
} 

//編譯器無法解析符號'result'和'c','c1'。錯誤。

你可能想查看其中數據是從顯示/活性XML文件中來:

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Enter a number" 
    android:textSize="15sp" 
    android:textColor="#ffffff" /> 


<EditText 
    android:id="@+id/editText1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:inputType="numberDecimal" 
    android:textColor="#ffffff"> 
</EditText> 

<RadioGroup 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:textColor="#ffffff"> 
    <RadioButton android:id="@+id/small" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Small answer" 
     android:textColor="#ffffff" 
     android:onClick="onRadioButtonClicked"/> 
    <RadioButton android:id="@+id/large" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Large answer" 
     android:textColor="#ffffff" 
     android:onClick="onRadioButtonClicked"/> 
</RadioGroup> 

<TextView 
    android:id="@+id/result" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="20sp" 
    android:textColor="#F9FF19" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Click for answer" 
    android:onClick="add" 
    android:textColor="#f9ff19" /> 
+1

您可能想要使用** ** ** EditText。只需添加一個CheckBox來設置「變體」。或者你可以使用2個RadioButton來使選擇更清晰。根據用戶的選擇,您將以「稍微不同」的方式處理輸入 – 2014-12-07 11:18:14

+0

感謝你的提問,我喜歡使用兩個RadioButtons爲用戶提供所需輸出(標準)的選擇。有這個活動的網站/代碼嗎? – ChiralCode 2014-12-07 11:46:28

+0

請參閱:http://developer.android.com/guide/topics/ui/controls/radiobutton.html – 2014-12-07 11:49:01

回答

0

我建議你聲明函數聲明以外的變量,因爲它似乎onRadioButtonClicked使用聲明的變量在添加方法中。一般來說,由於範圍問題,onRadioButtonClicked無法訪問c1,並且c2在裏面添加。

一個更好的辦法是聲明的變量,如MainActivity類的成員變量(通常我會設置他們的私人和使用getter/setter方法來訪問它們):

public class MainActivity extends ActionBarActivity { 
    TextView result; 
    EditText edit1; 
    double c; 
    double c1; 
    //your code 
} 

然後,你必須要知道,如果變量結果爲cc1在您單擊您的單選按鈕(這會觸發onRadioButtonClicked方法)之前沒有定義,您將遇到錯誤,因爲您正在對未定義的變量(如result.setText)執行操作。因此,你必須考慮函數被觸發的順序。我建議你在用戶選擇單選按鈕後觸發添加功能。例如:

public void add(){ 
    //code check for which radioButton is selected, or no radioButton is selected your code 
    //code to calculate according to whichever radioButton is selected your code 
    //display your code 
} 

這種方式,無需c選擇單選按鈕,c1,或result定義不會導致該應用關閉。

+0

感謝您的更新。根據您的建議將變量聲明爲類變量總體上工作得更好。我可以獲取代碼進行編譯,但當用戶單擊設備上所需的RadioButton時,應用程序會關閉。我嘗試嵌套時報告錯誤? add(){...}方法中的onRadioButtonClicked(View v){....}方法。也許我需要更多地嘗試使用代碼! – ChiralCode 2014-12-11 22:42:41

相關問題