2016-10-24 6 views
0

내가 편집 텍스트에 단어를 넣었을 때 숨겨진 버튼을 표시하고 싶다. 이 라인을 제외하고설정 버튼 안드로이드의 텍스트 워처에서 보이거나 보이지 않는다.

final EditText txtSearch = (EditText)findViewById(R.id.txtSearch); 
txtSearch.addTextChangedListener(new TextWatcher() { 
    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

    } 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     ((Button)findViewById(R.id.btnClear)).setVisibility(View.VISIBLE); 
     CategoryCustomAdapter adapter2 = 
       new CategoryCustomAdapter(showCategoryListActivity.this, 
         DatabaseHelper.getCategories(
           showCategoryListActivity.this,s.toString())); 
     ((ListView)findViewById(R.id.lstCategorylist)).setAdapter(adapter2); 
    } 

    @Override 
    public void afterTextChanged(Editable s) { 

    } 
}); 

모든 일이 잘 작동 :이 TextWatcher에서 내 코드

((Button)findViewById(R.id.btnClear)).setVisibility(View.VISIBLE); 

입니다 : 메신저는 이미이 방법을 시도

((Button)findViewById(R.id.btnClear)).setVisibility(View.VISIBLE); 

을 난 당신이 도움이 될 수 있습니다 확신 해요 나 밖으로. ps : API 19 Nexus 4 android 4.4.2

+0

당신이 당신의 버튼을하기 전에 할당 할 수 있습니다? 개인적으로 나는 afterTextChanged에 코드를 넣을 것이다. 그런 다음 btnClear.setvisibility (count> 0? VISIBLE : GONE);을 넣을 수 있습니다. – JCDecary

+0

그게 해결책이 아닌가 걱정됩니다. 하지만 당신이 말한 모든 일을 했는데도 아무 일도 없었습니다. (처음에는 강제로 닫습니다) –

+0

강제 종료 할 경우 여기에 logcat 로그를 게시 할 수 있습니까? 어떻게 그 라인을 제외한 모든 것이 잘 작동한다고 말할 수 있습니까? 내가 생각하는 유일한 이유는 "R.id.btnClear"이름이 잘못되었다는 것입니다. –

답변

0

앞서 --in Oncreate처럼 변수를 선언하십시오.

Button howtoButton = (Button) view.findViewById(R.id.howtobutton); 

결국 그것을 알아낼이

howtoButton.setVisibility(View.VISIBLE) 
howtoButton.setVisibility(View.GONE) 
+0

그랬지만이 오류가 어떻게 발생했는지 : –

+0

ImageButton 또는 일반 버튼을 사용하면 오류가 발생하는 이유를 결정해야합니다. 하나는 Cast ImageButton buttonname = (ImageButton) view.findViewById (R.id.ofbutton)이고 다른 하나는 Image 부분이없는 Button입니다. 귀하의 XML을 따르십시오 @ PedramHassas – skryshtafovych

+0

팁을 주셔서 고맙습니다. 죄송합니다. –

0

처럼 사용보다.
로그 말 :

이 android.widget 캐스트 할 수없는하여 ImageButton 을 android.support.v7.widget.AppCompat. 에서 onCreate()에 버튼

변경이이에

final Button btnClear = (Button)findViewById(R.id.btnClear); 

:

final ImageButton btnClear = (ImageButton)findViewById(R.id.btnClear); 
난하지만 난 내 버튼에 대한 몇 가지 배경을 사용하는 이유 몰라요

하고 회전하는 방법을 몇 가지 ~ ImageButtonbtnClear.setVisibility(View.VISIBLE);은 잘 작동합니다. public void onTextChanged(CharSequence s, int start, int before, int count)

버튼210
public class showCategoryListActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_show_category_list); 

     final ListView lstCategory = (ListView) findViewById(R.id.lstCategorylist); 
     final ImageButton btnClear = (ImageButton)findViewById(R.id.btnClear); 
     final EditText txtSearch = (EditText)findViewById(R.id.txtSearch); 

     CategoryCustomAdapter adapter = 
       new CategoryCustomAdapter(this,DatabaseHelper.getCategories(
         this,((TextView)findViewById(R.id.txtSearch)).getText().toString())); 
     lstCategory.setAdapter(adapter); 

     txtSearch.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       btnClear.setVisibility(View.VISIBLE); 
       CategoryCustomAdapter adapter2 = 
         new CategoryCustomAdapter(showCategoryListActivity.this, 
           DatabaseHelper.getCategories(
             showCategoryListActivity.this,s.toString())); 
       ((ListView)findViewById(R.id.lstCategorylist)).setAdapter(adapter2); 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 

      } 
     }); 
     txtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
      @Override 
      public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
       if (actionId == EditorInfo.IME_ACTION_SEARCH) { 
        CategoryCustomAdapter adapter = new CategoryCustomAdapter(
          showCategoryListActivity.this, 
          DatabaseHelper.getCategories(
            showCategoryListActivity.this, 
            ((TextView)findViewById(R.id.txtSearch)).getText().toString())); 
        ((ListView)findViewById(R.id.lstCategorylist)).setAdapter(adapter); 
        return true; 
       } 
       return false; 
      } 
      }); 
    } 


    public void btn_clear_clicked(View view) { 
     ((TextView)findViewById(R.id.txtSearch)).setText(""); 
     CategoryCustomAdapter adapter = new CategoryCustomAdapter(
       showCategoryListActivity.this, 
       DatabaseHelper.getCategories(
         showCategoryListActivity.this, 
         ((TextView)findViewById(R.id.txtSearch)).getText().toString() 
       ) 
     ); 
     ((ListView)findViewById(R.id.lstCategorylist)).setAdapter(adapter); 
    } 

} 

XML : 내 부주의에 대한

<ImageButton 
 
     android:id="@+id/btnClear" 
 
     android:layout_width="25dp" 
 
     android:layout_height="25dp" 
 
     android:text="Button" 
 
     android:layout_marginLeft="5dp" 
 
     android:layout_marginRight="5dp" 
 
     android:background="@android:drawable/ic_menu_close_clear_cancel" 
 
     android:layout_marginTop="11dp" 
 
     android:layout_alignParentTop="true" 
 
     android:layout_alignParentEnd="true" 
 
     android:onClick="btn_clear_clicked (showCategoryListActivity)" 
 
     android:visibility="invisible" 
 
     android:elevation="0dp" />

죄송합니다

+0

일단 문제가 해결되면 답변을 올리십시오. 코드 기반의 다른 문제 몇 가지. 1. 목록에 새 어댑터를 만들지 마십시오. 어댑터에서 사용하는 데이터를 새로 고치는 메서드를 만듭니다. 2. 어댑터를 클릭 수신기 외부의 ListView로 설정하십시오. 3. OnCreate에서 코드 초기화 만 수행하면됩니다. 나머지는 onStart를 사용하십시오. –