2012-08-06 64 views
0

我願意見強調像在ListView項目(我用的LinearLayout,而不是因爲ListView的不能使用的addChild()來動態地添加新的項目)做一個視圖高亮在LinearLayout中

當ListView項被觸摸,該項目通常突出顯示爲綠色。如何在線性佈局的視圖中實現此功能?

我試圖view.requestfocus在視圖的ontouchlistener.this返回true,但沒有任何東西可以被觀察到。

在此先感謝!

回答

1

您可以將項目添加到動態列表視圖....

public class MainActivity extends ListActivity { 

/** Items entered by the user is stored in this ArrayList variable */ 
ArrayList<String> list = new ArrayList<String>(); 

/** Declaring an ArrayAdapter to set items to ListView */ 
ArrayAdapter<String> adapter; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    /** Setting a custom layout for the list activity */ 
    setContentView(R.layout.main); 

    /** Reference to the button of the layout main.xml */ 
    Button btn = (Button) findViewById(R.id.btnAdd); 

    /** Defining the ArrayAdapter to set items to ListView */ 
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list); 

    /** Defining a click event listener for the button "Add" */ 
    OnClickListener listener = new OnClickListener() {   
     @Override 
     public void onClick(View v) {        
      EditText edit = (EditText) findViewById(R.id.txtItem); 
      list.add(edit.getText().toString()); 
      edit.setText("");    
      adapter.notifyDataSetChanged(); 
     } 
    }; 

    /** Setting the event listener for the add button */ 
    btn.setOnClickListener(listener); 

    /** Setting the adapter to the ListView */ 
    setListAdapter(adapter);   
} 

}

+0

謝謝你給我提供另一個solution.this是絕對有幫助的!但我還是想知道如何突出是實現。 – Will 2012-08-06 03:18:54