0

我希望像列表视图中的项目一样突出显示视图(我使用线性布局,因为列表视图不能使用 addchild() 动态添加新项目)

当一个列表视图项目被触摸时,该项目通常是绿色突出显示。如何将此功能实现到线性布局中的视图中?

我在视图的 ontouchlistener 中尝试了 view.requestfocus。这返回 true,但什么也看不到。

提前致谢!

4

1 回答 1

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);        
}

}

于 2012-08-06T03:07:10.170 回答