如何在我的情况下以编程方式添加按钮或其他元素? [英] How to add button or other element programatically in my case?

查看:48
本文介绍了如何在我的情况下以编程方式添加按钮或其他元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Panel pnl;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pnl = new Panel(this);
        setContentView(pnl);
    }
public boolean onPrepareOptionsMenu (Menu menu) {
        text_enter = menu.getItem(1);
        text_enter.setOnMenuItemClickListener(new OnMenuItemClickListener()
        {
            public boolean onMenuItemClick(MenuItem clickedItem)
            {
//Need to write something here to add button or other element programatically

                return true;
            }
        });

        return true;
    }
   class Panel extends SurfaceView implements SurfaceHolder.Callback {
	   private TutorialThread _thread;
        public Panel(Context context) {
            super(context);
            getHolder().addCallback(this);
            _thread = new TutorialThread(getHolder(), this);
            setFocusable(true);
        }
 
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            synchronized (_thread.getSurfaceHolder()) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                	if(menu_action == "traffic")
                	{
                		menu_action = "";
                		return true;
                	}

                    initial_x = event.getX();
                    initial_y = event.getY();
                }
                if (event.getAction() == MotionEvent.ACTION_UP) {

                }
                return true;
            }
        }
 
        @Override
        public void onDraw(Canvas canvas) {
            try{
            Bitmap img;
            GraphicObject.Coordinates cords;
            image.getCoordinates().setX(0);
            image.getCoordinates().setY(0);
            cords = image.getCoordinates();
            img = image.getGraphic();
            
            canvas.drawBitmap(img, cords.getX(), cords.getY(), null);
             }
             catch(Exception x){
            	 x.toString();
             }
            }
        }
 
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            // TODO Auto-generated method stub
        }
 
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            _thread.setRunning(true);
            _thread.start();
        }
 
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // simply copied from sample application LunarLander:
            // we have to tell thread to shut down & wait for it to finish, or else
            // it might touch the Surface after we return and explode
            boolean retry = true;
            _thread.setRunning(false);
            while (retry) {
                try {
                    _thread.join();
                    retry = false;
                } catch (InterruptedException e) {
                    // we will try it again and again...
                }
            }
        }
    }



请帮忙。


Please, help.

推荐答案

不要添加按钮 - 立即创建按钮并设置它可见(false)

然后,当调用 onMenuItemClick 时,设置按钮 visible(true)



*未授予Android编程中存在的可见(布尔值),但我想是的。它可能会被命名为不同。请自己检查一下。
DO not add button - create the button right away and set it visible(false).
Then, when the onMenuItemClick is called, set the button visible(true).

* not granted that visible(boolean) exists in Android programming, but I guess so. It might be named different though. Please check that out yourself.


这篇关于如何在我的情况下以编程方式添加按钮或其他元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆