Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单个页面中的多个按钮在android studio中将每个按钮导航到不同的页面_Java_Android_Android Layout - Fatal编程技术网

Java 单个页面中的多个按钮在android studio中将每个按钮导航到不同的页面

Java 单个页面中的多个按钮在android studio中将每个按钮导航到不同的页面,java,android,android-layout,Java,Android,Android Layout,请在android studio中解决这个问题,我是应用程序开发新手。 我想在一个页面中创建3个按钮,并将每个按钮导航到每个不同的页面。 我需要java的代码,即“Mainactivity.java” 我已经声明了3个按钮id 我已经在应用程序清单中设置了所有内容。 我一次只能导航一个按钮,但如何安排所有三个按钮进行导航 公共类MainActivity扩展AppCompatActivity实现View.OnClickListener{ @Override protected void onCre

请在android studio中解决这个问题,我是应用程序开发新手。 我想在一个页面中创建3个按钮,并将每个按钮导航到每个不同的页面。 我需要java的代码,即“Mainactivity.java” 我已经声明了3个按钮id 我已经在应用程序清单中设置了所有内容。 我一次只能导航一个按钮,但如何安排所有三个按钮进行导航

公共类MainActivity扩展AppCompatActivity实现View.OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button buttonWRGL = (Button)findViewById(R.id.buttonWRGL);
    Button buttonHNK = (Button)findViewById(R.id.buttonHNK);
    Button buttonKZP = (Button)findViewById(R.id.buttonKZP);

    buttonWRGL.setOnClickListener(this);
    buttonHNK.setOnClickListener(this);
    buttonKZP.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.buttonWRGL:

            break;
        case R.id.buttonHNK:
            break;
        case R.id.buttonKZP:
            break;
    }
}

}您的问题似乎不清楚,但我想您是在询问如何通过单击按钮加载另一个布局/活动/片段

这取决于你想做的三个动作中的哪一个:

1) 为了加载另一个布局,您需要在视图中对新布局进行充气;为了做到这一点,你需要清除实际布局和充气新的一个。 下面是一些示例代码:

//you may change it to whichever layout you used
LinearLayout ll = (LinearLayout) findViewById(R.id.mainLayout);

//remove previous view
ll.removeAllViews();

//set the new view
setContentView(R.layout.new_layout);
//create the new intent; it will refer to the new activity
Intent intent = new Intent(this, NewActivity.class);

//pass any data to the new activity; cancel this line if you don't need it
intent.putExtra(extra_title, extra)

//start the new activity
startActivity(intent);
//create the new fragment
Fragment newFragment = new MyFragment();

//start a fragment transaction
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

//replace the old fragment with the new
transaction.replace(R.id.frame, newFragment).commit();
buttonWRGL.setOnClickListener(new Button.OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent(this, NewActivity1.class);
        startActivity(intent);
    }
});
2) 如果你想开始一个新的活动,你需要使用一个意图并加载它。 示例代码:

//you may change it to whichever layout you used
LinearLayout ll = (LinearLayout) findViewById(R.id.mainLayout);

//remove previous view
ll.removeAllViews();

//set the new view
setContentView(R.layout.new_layout);
//create the new intent; it will refer to the new activity
Intent intent = new Intent(this, NewActivity.class);

//pass any data to the new activity; cancel this line if you don't need it
intent.putExtra(extra_title, extra)

//start the new activity
startActivity(intent);
//create the new fragment
Fragment newFragment = new MyFragment();

//start a fragment transaction
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

//replace the old fragment with the new
transaction.replace(R.id.frame, newFragment).commit();
buttonWRGL.setOnClickListener(new Button.OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent(this, NewActivity1.class);
        startActivity(intent);
    }
});
3) 若要更改片段,则需要执行一个事务。 示例代码:

//you may change it to whichever layout you used
LinearLayout ll = (LinearLayout) findViewById(R.id.mainLayout);

//remove previous view
ll.removeAllViews();

//set the new view
setContentView(R.layout.new_layout);
//create the new intent; it will refer to the new activity
Intent intent = new Intent(this, NewActivity.class);

//pass any data to the new activity; cancel this line if you don't need it
intent.putExtra(extra_title, extra)

//start the new activity
startActivity(intent);
//create the new fragment
Fragment newFragment = new MyFragment();

//start a fragment transaction
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

//replace the old fragment with the new
transaction.replace(R.id.frame, newFragment).commit();
buttonWRGL.setOnClickListener(new Button.OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent(this, NewActivity1.class);
        startActivity(intent);
    }
});
希望这有帮助;如果不是,试着编辑你的问题,以澄清你的意思


编辑:

您应该为每个按钮添加一个新的OnClickListener,但我的做法与您现在所做的不同。 我将执行以下示例代码:

//you may change it to whichever layout you used
LinearLayout ll = (LinearLayout) findViewById(R.id.mainLayout);

//remove previous view
ll.removeAllViews();

//set the new view
setContentView(R.layout.new_layout);
//create the new intent; it will refer to the new activity
Intent intent = new Intent(this, NewActivity.class);

//pass any data to the new activity; cancel this line if you don't need it
intent.putExtra(extra_title, extra)

//start the new activity
startActivity(intent);
//create the new fragment
Fragment newFragment = new MyFragment();

//start a fragment transaction
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

//replace the old fragment with the new
transaction.replace(R.id.frame, newFragment).commit();
buttonWRGL.setOnClickListener(new Button.OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent(this, NewActivity1.class);
        startActivity(intent);
    }
});
对于每个按钮。在这段代码中,我直接将一个特定的OnClickListener附加到按钮上;它将包含您需要的意图。
您可以在每个按钮(甚至10k按钮)中复制此内容,您只需更改您想要启动的活动的意图声明中的活动名称。

您可以编辑您的问题以显示您已经编写了哪些代码吗?谢谢“Tenik”,您对我来说有些清楚。我想使用intent打开新活动,我可以从一个按钮打开新活动,但我如何才能将intent用于所有三个按钮,以便我可以从第一个按钮打开一个新活动,第二个按钮指向另一个新活动,第三个按钮指向另一个新活动。再次感谢。我有了一个清晰的想法,将按照您指导的方式进行尝试。好:)如果它有效,请将答案加粗为“正确”:)非常感谢它的工作很好,我已为每个按钮设置了OnClickListener。