单击按钮时如何显示新屏幕?

时间:2013-04-13 18:30:30

标签: android

我正在编写我的第一个Android应用程序,并且我希望在单击按钮时在新屏幕上显示结果。我搜索了一下,发现我需要多个活动或布局。 但是我仍然没有得到我应该做的事情(我之前没有使用Android开发)。我想在点击按钮时显示一个全新的屏幕,我该怎么办?

我希望有人能说出明确的步骤。

4 个答案:

答案 0 :(得分:4)

使用Intent在两个Activity之间切换。

Intent inn1=getIntent();
inn1=new Intent(HomeActivity.this,SecondActivity.class);
startActivity(inn1);

首先在android中创建一个简单活动,并在main.xml中添加按钮。然后使用findViewById方法找到按钮的ID。然后为代码添加 OnClick 方法,并将意图代码放入Onclick方法中。并且不要忘记在清单文件中输入课程。

答案 1 :(得分:1)

请确保将其与.xml布局相关联

//Add the variable to avoid any errors
Button goToAnotherClass;

goToAnotherClass = (Button) findViewById(R.id.anotherclassbutton); //find the button by its assigned id

//Make the button do something:
goToAnotherClass.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent myIntent = new Intent(CurrentClass.this,
                    TheClassIWantToGoTo.class);
            startActivity(myIntent);

        }
    });

如果您仍在努力,请观看this视频。

答案 2 :(得分:0)

听起来你需要查看Android Activity课程。您要求的众多实现之一将是为您的应用程序的每个屏幕使用一个Activity。

这是您开始新活动的方式(取自here)。

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

答案 3 :(得分:0)

在这里,您可以找到有关如何实现这一目标的更多信息:Writing your first app - Start another activity

相关问题