有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android点击按钮进入另一个页面?

我需要将按钮链接到页面(而不是主页),比如我点击按钮进入页面(位置页面)

private void setupLocationPageButton() {

    Button LocationPageButton = (Button) findViewById(R.id.btnLocationPage);
    LocationPageButton.setOnClickListener(new View.OnClickListener()

共 (3) 个答案

  1. # 1 楼答案

    如果你的目标是开始另一项活动,这就是你想要做的

    在xml文件中,您需要这样的内容

    ...
    
    <Button
    android:id="@+id/activity_button"
    android:layout_width="wrap_content"    
    android:layout_height="wrap_content"
    android:onClick="onButtonClicked"
    android:text="@string/text_of_button" />
    
    ...
    

    然后在你的活动中你想要

    public void onButtonClicked(View view) {
    
        Intent intent = new Intent(this, OtherActivity.class);
        startActivity(intent);
    
    }
    
  2. # 2 楼答案

    你可以使用fragment,每个fragment都包含一个特定的页面,请参见thisfor Android fragment

  3. # 3 楼答案

    我想你想做的是:当你点击按钮时,它会改变主要活动


    Button LocationPageButton = (Button) findViewById(R.id.btnLocationPage);
    LocationPageButton.setOnClickListener(new View.OnClickListener({
         public void onClick(View _view) {
         Intent i = new Intent(MainActivity.this,TheActivityTheclassNameYouWannGoTo.class);
         startActivity(i);
         }
    }));
    

    但首先你必须创建活动,并像MainActivity那样继承活动


    在AndroidMainfest中初始化类。xml


    我希望这对你有帮助