1

我有一个相对做同样事情的 Clickable TextViews 列表。你点击它,它就会进入那个活动。设置转到设置活动。约到约等。除了这个重复的代码之外,还有更简单的方法来声明和设置这些可点击的 TextView 吗?

 TextView create,
                         edit,
                         settings,
                         about;

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

                create = (TextView) findViewById(R.id.create);
                        create.setTextColor(Color.parseColor("#000000"));
                edit = (TextView) findViewById(R.id.edit);
                        edit.setTextColor(Color.parseColor("#000000"));
                settings = (TextView) findViewById(R.id.settings);
                        settings.setTextColor(Color.parseColor("#000000"));
                about = (TextView) findViewById(R.id.about);
                        about.setTextColor(Color.parseColor("#000000"));

                create.setOnClickListener(new View.OnClickListener(){
                        @Override
                        public void onClick(View v) {

                        }      
                });

                edit.setOnClickListener(new View.OnClickListener(){
                        @Override
                        public void onClick(View v){

                        }
                });

                settings.setOnClickListener(new View.OnClickListener(){
                        @Override
                        public void onClick(View v){

                        }
                });

                about.setOnClickListener(new View.OnClickListener(){
                        @Override
                        public void onClick(View v){

                        }
                });
4

2 回答 2

1

如果您有一小部分项目,您可以采取以下方法:

public class MyActivity extends Activity implements AdapterView.OnItemClickListener {

    private ArrayAdapter<Item> mAdapter;

    private static enum Item {Create,Edit,Settings,About}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //-- can set up from external layout also--
        ListView list = new ListView(this);
        setContentView(list);

        list.setOnItemClickListener(this);

        mAdapter = new ArrayAdapter<Item>(this,android.R.layout.simple_list_item_1,Item.values());
        list.setAdapter(mAdapter);

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        switch (mAdapter.getItem(i)){
            case Create:
                //--do stuff--
                break;
            case Edit:
                //--do stuff--
                break;
            case Settings:
                //--do stuff--
                break;
            case About:
                //--do stuff--
                break;
        }
    }
}
于 2013-01-27T05:39:23.110 回答
0

您可以实现View.OnClickListener

   public class YOURACTIVITY extends Activity implements OnClickListener

在 onCreate()-

   yourTextview.setOnClickListener(this);

进而

@Override
public void onClick(View v) {
    // Based on the view , set the action


}
于 2013-01-27T04:07:02.090 回答