gpt4 book ai didi

android - 按按钮转到新屏幕

转载 作者:行者123 更新时间:2023-11-29 01:43:04 26 4
gpt4 key购买 nike

这是我第一次在 Android 中开发。我正在尝试创建一个按钮,该按钮在按下时应打开一个新屏幕。我尝试按照 Android 教程进行操作,并阅读了本网站上的一些问题,但我仍在苦苦挣扎。抱歉,如果这个问题对其他人来说是多余的。

到目前为止我得到了这个:

MainActivity.java

package com.example.squashbot;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {

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

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {


public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}

public void scoreGame (View view)
{
Intent intent = new Intent(getActivity(), ScoreNewGame.class);

startActivity(intent);
}
}

}

fragment_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<Button
android:id="@+id/score_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/score_button"
android:onClick="scoreGame"/>

</LinearLayout>

ScoreNewGame.java

package com.example.squashbot;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class ScoreNewGame extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Intent intent = getIntent();
setContentView(R.layout.activity_score_new_game);

}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_score_new_game,
container, false);
return rootView;
}
}

}

fragment_score_new_game.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

</RelativeLayout>

我对这个还很陌生,所以慢慢来。提前感谢您的帮助,非常感谢

最佳答案

如果您按下按钮,应用程序会崩溃吗?然后,这是因为 android:onClick="scoreGame" 属性中的 scoreGame 方法(在您的 Button 中到 fragment_main.xml) 必须附加到您的 Activity 而不是 Fragment,如下所示:

    //...

public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}

}
// end of PlaceholderFragment

public void scoreGame (View view) {
Intent intent = new Intent(this, ScoreNewGame.class);
startActivity(intent);
}

}
// end of MainActivity

注意:您应该发布堆栈跟踪,它会更好地帮助您。希望这对您有所帮助。

关于android - 按按钮转到新屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23186902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com