在线性布局中突出显示视图

时间:2012-08-06 02:52:36

标签: android

我希望将视图突出显示为listview中的项目(我使用linearlayout而不是因为listview不能使用addchild()来动态添加新项目)

触摸列表视图项时,该项通常会突出显示为绿色。如何在线性布局中将此功能实现到视图中?

我在视图的ontouchlistener中尝试了view.requestfocus.this返回true但没有任何东西可以观察到。

提前致谢!

1 个答案:

答案 0 :(得分: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);        
}

}