2013-04-08 210 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