2014-06-20 57 views
4

我正在開發示例應用程序來測試OnFocusChangeListener,我保留調試點上它的方法public void onFocusChange(View v, boolean hasFocus),我觀察到該方法只調用一次,即啓動應用程序後,直到這很好,但當我向下滾動到下一個列表項目,該項目被集中,但方法不被調用。我知道我錯過了一點點。你能幫我擺脫這個問題嗎?提前致謝。方法onFocusChangeListener只調用一次

注意滾動通過使用遙控器的完成d-墊

java類:

package com.example.listview; 

import android.app.Activity; 
import android.graphics.Rect; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnFocusChangeListener; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

public class ListActivity extends Activity implements OnFocusChangeListener{ 
    String[] items={"iOS","android","Symbian",}; 
    //GridView lst; 
    ListView lst; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_list); 

     lst=(ListView)findViewById(R.id.listView1); 

     lst.setFocusableInTouchMode(false); 

     ArrayAdapter<String> adapter=new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1,items); 
     lst.setAdapter(adapter); 


     } 



    public void onFocusChange(View v, boolean hasFocus) { 

     Toast.makeText(getBaseContext(), "FOCUSED", Toast.LENGTH_LONG).show();  
    } 
} 

回答

1

我找到解決問題的方式,但我仍然不知道爲什麼OnFocuschangeListener不工作?我解決它通過實現OnItemSelectedListener

package com.example.listview; 

import android.app.Activity; 
import android.graphics.Rect; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnFocusChangeListener; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

public class ListActivity extends Activity implements OnItemSelectedListener{ 
    String[] items={"iOS","android","Symbian",}; 
    //GridView lst; 
    ListView lst; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_list); 

     lst=(ListView)findViewById(R.id.listView1); 

     lst.setFocusableInTouchMode(false); 

     ArrayAdapter<String> adapter=new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1,items); 
     lst.setAdapter(adapter); 


     } 



    @Override 
public void onItemSelected(AdapterView<?> parent, View view, int position, 
     long id) 
} 
3

焦點被稱爲當你使用跟蹤球或遠程使其突出(不接觸。一個例子是在Android電視,您與左導航,上,下, )

您需要一個onItemSelected進行選擇。 onItemClickListener進行點擊。

+1

@謝謝你的回答,我用貼子回答了。 ..非常感謝你 –