Android的对话活动Android

2023-09-06 11:17:57 作者:一见倾心じòぴé

我需要帮助的以下内容:结果当我触摸我的mainActivity,应该处理onTouch事件,并启动新的活动是一个对话的活动。我不能做到这一点。任何人都可以提出什么吗?

我添加安卓主题=@安卓风格/ Theme.Dialog。结果如果我设计简单的对话活动,然后它工作正常,但如果我试图打开它的触摸事件,然后它不工作。

 公共布尔onTouch(视图V,MotionEvent事件){    INT行动= event.getAction();    如果(动作== MotionEvent.ACTION_DOWN){        意图loginIntent =新意图(这一点,Login.class);        startActivity(loginIntent);        返回true;    }    返回true;} 

解决方案

您必须通过上下文的intent.and你刚才写的本。而你在侦听器,它通过监听器不是上下文你activity.so你需要在那里写YourActivity.this。所以替换此行

 意图loginIntent =新意图(这一点,Login.class); 
Facebook Message 与 Twitter 都在让对话通知符合 Android 11 新特性

与此一

 意图loginIntent =新意图(YourActivity.this,Login.class); 

和你done.hope有所帮助。

I need help on following: When I touch on my mainActivity, it should handle onTouch event and start new activity which is a dialog activity. I am not able to do it. Can anyone suggest anything ?

I add android:theme="@android:style/Theme.Dialog". If I design simple Dialog activity then it works fine but if I am trying to open it on touch event then it's not working.

public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    if(action == MotionEvent.ACTION_DOWN) {         
        Intent loginIntent = new Intent(this, Login.class);
        startActivity(loginIntent);
        return true;
    }
    return true;
}

解决方案

you have to pass the context to your intent.and you have just wrote "this".and you are in Listener so it passes the Listener not the context of your activity.so you need to write YourActivity.this there. so replace this line

 Intent loginIntent = new Intent(this, Login.class);

with this one

 Intent loginIntent = new Intent(YourActivity.this, Login.class);

and you are done.hope this helps.

 
精彩推荐
图片推荐