声明TextView列表的更好方法是什么?

时间:2013-01-27 03:44:49

标签: java android textview

我有一个可点击的TextView列表,它们相对做同样的事情。你点击它就会进入那个活动。设置转到设置活动。即将到来等等。除了这个重复的代码之外,还有一种更简单的方法来声明和设置这些可点击的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){

                        }
                });

2 个答案:

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

答案 1 :(得分: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


}