Android的:如何通过按钮启动的活动? [英] Android: How to start an Activity via button?

查看:91
本文介绍了Android的:如何通过按钮启动的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新手上路Android开发者在这里我比code更多的图形,但想我会开始做更多的编码。不管怎么说,我有我的主要活动网页一些按钮,我想点击它打开另一个类/活动的按钮,所以当。我已经尝试了所有我抬头一看,事情仍然没有工作,当我点击模拟器,它只是没有做任何事情的按钮,简化版,forcestop这样或任何东西根本不值一提的方法,有人点我在正确的方向吧。

从主页面的按钮住code:

 公共类起点延伸活动{
保护无效的onCreate(捆绑的目的){
    super.onCreate(AIM);

    的setContentView(R.layout.main);

    最终按钮BSL =(按钮)findViewById(R.id.bSongList);
    bSL.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){

            意图的SongList =新的意图(StartingPoint.this,SongList.class);
            StartingPoint.this.startActivity(的SongList);
        }
    });
}
}
 

清单

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=myname.appname
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <使用-SDK安卓的minSdkVersion =10/>

    <应用
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@安卓风格/ Theme.Black.NoTitleBar>
        <活动
            机器人:名称=泼水节
            机器人:标签=@字符串/ APP_NAME
            机器人:主题=@安卓风格/ Theme.Black.NoTitleBar.Fullscreen>
            <意向滤光器>
                <作用机器人:名称=myname.appname.SPLASH/>

                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动
            机器人:起点NAME =
            机器人:标签=@字符串/ APP_NAME>
            <意向滤光器>
                <作用机器人:名称=myname.appname.STARTINGPOINT/>

                <类机器人:名称=android.intent.category.DEFAULT/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动
            机器人:的SongListNAME =
            机器人:标签=@字符串/ APP_NAME>
            <意向滤光器>
                <作用机器人:名称=myname.appname.SONGLIST/>

                <类机器人:名称=android.intent.category.DEFAULT/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>

< /舱单>
 

在先进的感谢!

-L

 只需点击有关按钮后,最后3行logcat中的。

01-02 21:59:50.473:I / ActivityManager(75):开始:意向{CMP = myname.appname / .SongList}从PID 681
01-02 21:59:52.953:I / ActivityManager(75):显示myname.appname / .SongList:+ 2s351ms
01-02 21:59:58.565:D / dalvikvm(348):GC_EXPLICIT释放8K,55%免费2591K / 5703K,外部1625K / 2137K,暂停520ms
 

解决方案

试试这个:

 意向的SongList =新的意图(StartingPoint.this,SongList.class);
startActivity(的SongList);
 

您是否收到任何错误?您需要添加您在清单中创建的每一个活动。如果你有两个活动,并仅在主清单中,这可能是你的问题。

在第二个想法,我相信你的清单是错误的。查看这个。随着主(起点)和菜单这是第二个:

 <活动机器人:名称=com.activities.Main>
        <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>
                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
    < /活性GT;
    <活动机器人:名称=com.activities.Menu>< /活性GT;
 

试试这个清单:

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=myname.appname
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <使用-SDK安卓的minSdkVersion =10/>

    <应用
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@安卓风格/ Theme.Black.NoTitleBar>
        <活动
            机器人:名称=泼水节
            机器人:标签=@字符串/ APP_NAME
            机器人:主题=@安卓风格/ Theme.Black.NoTitleBar.Fullscreen>
            <意向滤光器>
                    <作用机器人:名称=android.intent.action.MAIN/>
                    <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动
            机器人:起点NAME =
            机器人:标签=@字符串/ APP_NAME>
        < /活性GT;
        <活动
            机器人:的SongListNAME =
            机器人:标签=@字符串/ APP_NAME>
        < /活性GT;
    < /用途>

< /舱单>
 

您不需要添加过滤器上的每一项活动。我明白,你只有一个条目的活动,那就是闪屏。但是,您可能需要更改舱单更改.Splash和所有其他活动的完整路径包括包。

Noob Android developer here I'm more graphics than code but thought I'd start doing more coding. Anyway, I have some buttons on my main activity page and I want it so when the button is clicked it opens another class/activity. I've tried all the methods I've looked up and something is still not working, when I click the button in the emulator it just doesn't do anything, does't forcestop or anything just nothing, someone point me in the right direction please.

Code from the main page where the button lives:

public class StartingPoint extends Activity {
protected void onCreate(Bundle aim) {
    super.onCreate(aim);

    setContentView(R.layout.main);

    final Button bSL = (Button) findViewById(R.id.bSongList);
    bSL.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            Intent SongList = new Intent(StartingPoint.this, SongList.class);
            StartingPoint.this.startActivity(SongList);
        }
    });
}
}

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="myname.appname"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar" >
        <activity
            android:name=".Splash"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
            <intent-filter>
                <action android:name="myname.appname.SPLASH" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".StartingPoint"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="myname.appname.STARTINGPOINT" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SongList"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="myname.appname.SONGLIST" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Thanks in advanced!

-L

Last 3 lines of logcat just after clicking the button in question.

01-02 21:59:50.473: I/ActivityManager(75): Starting: Intent { cmp=myname.appname/.SongList } from pid 681
01-02 21:59:52.953: I/ActivityManager(75): Displayed myname.appname/.SongList: +2s351ms
01-02 21:59:58.565: D/dalvikvm(348): GC_EXPLICIT freed 8K, 55% free 2591K/5703K,     external 1625K/2137K, paused 520ms

解决方案

Try this:

Intent songList = new Intent(StartingPoint.this, SongList.class);
startActivity(songList);

Are you getting any errors? You need to add every Activity you create in the manifest. If you have two activities and only the Main in the manifest, that could be you problem.

On second thought, I believe your manifest is wrong. Check this. With the Main (Your starting point) and the Menu which is the second one:

<activity android:name="com.activities.Main">
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>  
    </activity>
    <activity android:name="com.activities.Menu"></activity>

Try this manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="myname.appname"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar" >
        <activity
            android:name=".Splash"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
            <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />               
            </intent-filter>
        </activity>
        <activity
            android:name=".StartingPoint"
            android:label="@string/app_name" >           
        </activity>
        <activity
            android:name=".SongList"
            android:label="@string/app_name" >            
        </activity>
    </application>

</manifest>

You don't need to add the filter on each activity. I understood that you have only one entry Activity and that is the splash screen. However, you might need to change the manifest to change the ".Splash" and every other Activity to the full path including the package.

这篇关于Android的:如何通过按钮启动的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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