5

Android开发,能不能把做出来的功能封装成类,以后需要的时候直接调用这个类来实现这个功能

比如我写了个具有图文的Toast,我想把它封装成类,以后要用这种toast的时候直接调用。代码如下
private Button mButton;

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

    mButton = (Button) findViewById(R.id.button1);
    mButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            ImageView mView = new ImageView(MainActivity.this);
            TextView mTextView = new TextView(MainActivity.this);

            LinearLayout layout = new LinearLayout(MainActivity.this);
            mTextView.setText("SS");
            Linkify.addLinks(mTextView, Linkify.WEB_URLS
                    | Linkify.PHONE_NUMBERS | Linkify.EMAIL_ADDRESSES);
            Toast toast=Toast.makeText(MainActivity.this, mTextView.getText(), Toast.LENGTH_LONG);

            View textView=toast.getView();
            layout.setOrientation(LinearLayout.HORIZONTAL);
            mView.setImageResource(R.drawable.ic_launcher);

            layout.addView(mView);
            layout.addView(textView);
            toast.setView(layout);
            toast.show();
        }
    });
}

如何封装成类???
查看全部
qq_20401801
无夜之星辰
2015/09/01 06:14
  • java
  • android
  • eclipse
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复