Eclipse - ButtonCLick -->有文字的新屏幕?

发布于 2024-09-18 23:36:43 字数 224 浏览 4 评论 0原文

我对 Android 开发还很陌生,我只完成了诸如 hello world、paint pot 等应用程序。

我知道如何设计布局,但当涉及到“活动”时,我总是把事情搞砸。

所以现在对于我的问题 - 我正在创建一个应用程序来显示我的学校时间表,所以我有 5 个按钮(周一周二周三等) 然后,当我单击每个按钮时,我想进入具有今天日程安排的另一个屏幕。

如何以简单的方式创建新屏幕? 友善一点

Im kinda new on developing for Android, and i've only completed apps like - hello world, paint pot etc.

I know how to design the layout, but when it comes to the "activity" I alwats messes things up..

So now to my question - I'm creating a app to show my school schedule so I have 5 buttons (monday tuesday wednesday etc.)
Then when I click each button I want to get to another screen with todays schedule..

How to I create new screens in an easy way?
Be kind

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三五鸿雁 2024-09-25 23:36:43

下次您发布内容时,请确保包含一段代码。这样我们就可以轻松地帮助您。

关于你的问题......你要做的就是从主要活动中打开一个新活动。这是通过使用意图和 startActivity 方法来完成的。我将为您提供一个简单的示例,其中只有一天(最好的一天,星期五!):

public class SchoolActivity extends Activity{
    public void onCreate(Bundle b){
        super.onCreate(b);
        setContentView(R.layout.shool_layout);

        // you have initialized your buttons here

        // let's suppose this is the reference to your friday button
        btnFriday.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // launch intent for friday
                launchDay(DayDetails.FRIDAY);
            }
        });
    }

    private void launchDay(String whichDay){
        Intent intent = new Intent(SchoolActivity.this, DayDetails.class);
        intent.putExtra(DayDetails.DAY, whichDay);
        startActivity(intent);
    }
}

然后,在您的当天活动中,您将显示指定日期的详细信息:

public class DayDetails extends Activity{
    public static final String DAY = "day";
    public static final String FRIDAY = "friday";

    public void onCreate(Bundle b){
        super.onCreate(b);
        setContentView(R.layout.daylayout);

        Bundle extras = getIntent().getExtras();

        if( extras.getString(DAY).equals(FRIDAY) ){
            // show things for the friday
        }
    }
}

请注意,您必须创建两个布局文件在 res/layout 文件夹中;一个用于主布局(在本例中为 school_layout.xml),另一个用于当天的详细信息(daylayout.xml)。希望这个示例对您有所帮助,并让您了解在这些情况下如何继续操作。

The next time you post something, make sure to include a snippet of code. That way we can easily help you.

With regards to your question... what you have to do is open a new activity from the main activity. That's done by using intents and the startActivity method. I'm gonna give you a simple example where there is only one day (the best day, friday!):

public class SchoolActivity extends Activity{
    public void onCreate(Bundle b){
        super.onCreate(b);
        setContentView(R.layout.shool_layout);

        // you have initialized your buttons here

        // let's suppose this is the reference to your friday button
        btnFriday.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // launch intent for friday
                launchDay(DayDetails.FRIDAY);
            }
        });
    }

    private void launchDay(String whichDay){
        Intent intent = new Intent(SchoolActivity.this, DayDetails.class);
        intent.putExtra(DayDetails.DAY, whichDay);
        startActivity(intent);
    }
}

Then, on your day activity, you will show details for the specified day:

public class DayDetails extends Activity{
    public static final String DAY = "day";
    public static final String FRIDAY = "friday";

    public void onCreate(Bundle b){
        super.onCreate(b);
        setContentView(R.layout.daylayout);

        Bundle extras = getIntent().getExtras();

        if( extras.getString(DAY).equals(FRIDAY) ){
            // show things for the friday
        }
    }
}

Notice that you will have to create two layout files on the res/layout folder; one for the main layout (in this case school_layout.xml) and the other for the day details (daylayout.xml). Hope this example help you and give you an idea of how to proceed in these cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文