在安卓系统中从一个视图中启动一个活动?[英] Starting an activity from a view in android?

本文是小编为大家收集整理的关于在安卓系统中从一个视图中启动一个活动?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

有没有办法从视图开始活动?我有一个很好的视图来呈现这些菜单按钮,当他们点击一个特定的按钮(被困在一个 onTouch 事件中)时,我想开始"点击"​​活动.

从我看来,startActivity 方法似乎无法调用.所以这让我相信没有办法做到这一点,在这种情况下,我想我是在问,当我希望此菜单具有动画可绘制对象的视图并且可以触摸以知道什么"按钮"时,我在这里做什么" 被要求知道要开始什么活动?

我一直在寻找,但似乎没有什么符合我的要求.我怀疑因为我找不到它可能会以某种方式打破惯例,在这种情况下,这样做的正确方法是什么?

我有另一个活动可以启动这个很酷的滚动瓷砖地图视图.当用户单击特定的磁贴时,我想根据单击的磁贴 ID 加载完全不同的视图(甚至可能不确定我需要哪个活动).这个新的视图或活动将显示被点击的单元格中的所有项目(想象一下带有单元格的地图,每个单元格都有内容.当用户点击单元格时,会发生一个新视图,将所有这些内容显示为很酷的可绘制对象,就像它在单元格上"放大"一样).

我认为我需要同样的东西,当用户在我的滚动地图视图中并且他们按下然后向上我得到他们点击的单元格并使用它来初始化将启动的活动显示放大状态事物(通过使用我怀疑的新型视图的 drawable)

推荐答案

有没有办法开始一个活动从一个角度来看?

可以说,View 应该通过注册的回调方法告诉承载它的 Activity 启动一个 Activity.我不喜欢您在此处描述的代码组织(但无可否认,仅基于描述).

但是,如果您真的想让 View 启动活动,请让 View 调用 getContext().startActivity().

其他推荐答案

您确实使用 startActivity 启动活动.

// first create the intent
Intent intent = new Intent ( this,  ClickedActivity.class );
// start the activity
this.startActivity( intent );

本文地址:https://www.itbaoku.cn/post/102746.html

问题描述

Is there a way to start an activity from a view? I have this nice view that renders these menu buttons and when they click a particular button (Trapped in an onTouch event) I want to start the "clicked" activity.

It seems that there from my view startActivity method is uncallable. So that leads me to believe that there is no way to do this, in which case I guess I am asking, what do I do here when I want this menu to have a view that animates drawables and can get touches to know what "button" was pressed to know what activity to start?

I have been searching and nothing seems to fit what I am looking for. I suspect since I cannot find it that it probably breaks convention in some way and in which case, what is the proper way to do this?

I have another Activity that starts this cool scrolling tile map view. When the user clicks on a specific tile I would like to load up an entirely different view (maybe even activity not sure which I need) based on the tileID clicked. This new View or activity would show all the items in the cell that was clicked (imagine the map with cells , each cell has stuff in it. When the user clicks on the cell then a new view happens that shows all this stuff as cool drawables, like it "zoomed in" on the cell).

I would think I need the same thing, when the user is inside my scrolling map view and they push down then up I get what cell they clicked on and use that to initialize the activity that will launch showing the zoomed in state of things (via drawables using a new kind of view i suspect)

推荐答案

Is there a way to start an activity from a view?

Arguably, the View should tell the Activity that hosts it to start an activity, via a registered callback method. I am not a fan of the code organization you have described here (but, admittedly, based solely on the description).

However, if you really want to have the View start the activity, have the View call getContext().startActivity().

其他推荐答案

you do start an activity using the startActivity.

// first create the intent
Intent intent = new Intent ( this,  ClickedActivity.class );
// start the activity
this.startActivity( intent );