2013-04-08 136 views
0

在我的節目我有什麼是二AutoCompleteTextView一個ButtonTableLayout表現出了一定的成績。在這個程序中,TableLayout是隱藏的,只有在生成結果後纔會顯示。隱藏按鈕

我也希望能讓Button只在用戶填寫AutoCompleteTextView後纔出現,但是在我寫的代碼中,我寫的代碼沒有按照我希望的方式運行。

calculate = (Button)findViewById(R.id.calculate); 
    if(location.getText().toString().length()<1 && destination.getText().toString().length()<1) 
    { 
     calculate.setVisibility(View.INVISIBLE); 
    } 
    else 
    { 
     calculate.setVisibility(View.VISIBLE); 
     calculate.setOnClickListener(new View.OnClickListener() { 



      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

      if(location.getText().toString().length()<1 || destination.getText().toString().length()<1) 
      { 
       Toast.makeText(getApplicationContext(), "Give values before placing a query", Toast.LENGTH_LONG).show(); 
      } 
      else{ 
       @SuppressWarnings("deprecation") 
       String url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins="+URLEncoder.encode(location.getText().toString())+"&destinations="+URLEncoder.encode(destination.getText().toString())+"&unit=metric&mode=driving&sensor=false"; 
       new ReadDistanceJSONFeedTask().execute(url); 
       z=+1; 
       isNormal=false; 
       supportInvalidateOptionsMenu(); 
       } 
      } 
     }); 

    } 

在此代碼Button根本不會出現在所有。有人可以幫忙嗎?提前致謝。

+1

這是哪裏的代碼片段位於何處? – 2013-04-08 14:44:15

+0

在OnCreate() – 2013-04-08 14:45:06

+1

你只計算你的按鈕知名度一次,在開始時,當文本肯定是空的,所以按鈕消失再也沒有回來。聽起來像你應該聽取文本中的變化,並在發生時計算你的按鈕可見性。 – Oren 2013-04-08 14:45:17

回答

1

你只計算一次,你的按鈕知名度,在開始時,當文本肯定是空的,所以按鈕消失永遠不會返回。聽起來像你應該listen改變文本和計算你的按鈕的可見性,當他們發生。

喜歡的東西(假設你的AotoCompleteTextView名爲BOB):

bob.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // TODO your button calculation goes here    
     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) {} 

     @Override 
     public void afterTextChanged(Editable s) {} 
    }); 
+0

就像我提到的......有兩個自動完成,只有後兩者真的應該按鈕出現......我很抱歉,如果我纏着你這樣做,因爲我是一個完整的noob,怎麼能我檢查兩個AutoCompletes是否被處理? – 2013-04-08 15:02:45

+0

我的頭頂?在他們兩個上註冊相同的TextWatcher,並在其中檢查兩者的狀態。所以當更改文字時,您的支票將會運行。 – Oren 2013-04-08 15:25:08

0

我想你可能犯了一個錯誤,試圖改變

if(location.getText().toString().length()<1 && destination.getText().toString().length()<1)

if(location.getText().toString().length()<1 || destination.getText().toString().length()<1)

+0

沒有工作...... – 2013-04-08 14:48:09

+0

OK也許不使用的toString()?什麼是location.getText()的返回類型? – Robin 2013-04-08 14:50:09

+0

的的inputType是一個文本 – 2013-04-08 14:52:13