2015-03-02 109 views
0

希望你們一切安好。如果EditText字段小於或等於13.5且大於33.0,則隱藏一個按鈕 - Java Android

如果我的EditText中的測試讀數在我的Android應用程序窗口中低於或等於13.5或大於33,我想隱藏一個按鈕(btnAppointment)。如果我的EditText字段輸入值介於13.60和32.99之間,Button(btnAppointment)應該只出現在屏幕上。如果超出這些參數,則不會在屏幕上顯示。

我想知道是否與button.setEnabled(false); IF語句將做的伎倆,如果是這樣,我需要輸入到我的代碼。無論是protected void onCreate(Bundle savedInstanceState)或創建我自己的public void appointmentTeacherOnClick

下面我插入了我的代碼,用於計算和顯示我的輸入測試字段提示。

public void calculateTest(View v){ 
    String status; 
    test = Double.parseDouble(edtData.getText().toString()); 
    String result = String.format("%.2f", test); 
    Log.d("MyActivity", result); 

    if(test < 9.5) { 
     status = "Normal - Well Done =)"; 
    } else if (test >= 9.5 && test < 13.5){ 
     status = "Caution - Keep on Track =|"; 
    } else if (test >= 13.5 && test < 33.0) { 
     status ="Action Needed =("; 
    } else { 
     status = "Normal Results are between 0 - 33"; 
    } 

    AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
    alertDialog.setTitle("Result Feedback..."); 
    alertDialog.setMessage(status); 
    alertDialog.setButton("Acknowledged", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
     } 
    }); 
    alertDialog.show(); 

回答

1

如果你想控制按鈕的可見性,啓用或禁用按下按鈕將無濟於事。啓用/禁用屬性仍然會顯示按鈕,只會決定是否可以點擊按鈕。

你需要兩樣東西來實現自己的任務,

  1. 的EditText - 文本更改偵聽
  2. 按鈕Visibility屬性

如何執行兩個任務都已經回答了所以,這裏是最受歡迎的一個,

How to hide a button programmatically?(用於隱藏按鈕)。

Counting Chars in EditText Changed Listener(有關創建的EditText變化監聽)

+0

感謝您的提示答案......真正欣賞它 – 2015-03-02 22:23:30

1

要隱藏你需要使用setVisibility()並將其設置爲View.INVISIBLE

Button btnAppointment = (Button) findViewById(R.id.btn_appointment); 
btnAppointment.setVisibility(View.INVISIBLE); 

在你的XML文件中的觀點:

<Button 
    android:id="@id+/btn_appointment 
    ... 
/> 

看到這個問題:How to hide a button programmatically?

+0

感謝您的提示答案......真正欣賞它 – 2015-03-02 22:23:36

相關問題