【问题标题】:android button onClick not transitioning to new activityandroid按钮onClick没有转换到新活动
【发布时间】:2016-08-23 21:44:51
【问题描述】:

所以我对 Android 开发非常陌生,这可能是一个简单的问题,我错过了一件简单的事情。我有一堂课,我有一个按钮,我试图单击一个按钮并转换到另一个活动。

public class ContentProfile extends AppCompatActivity  {

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

    }

    public void settingsPage(View v){
        Intent intent = new Intent(ContentProfile.this, ContentSettings.class);
        startActivity(intent);
    }

}

我正在尝试打开 ContentSetting 类。 ContentProfile 的 XML 是:

    <Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Settings"
    android:layout_alignParentRight="true"
    android:id="@+id/button11"
    android:onClick="settingsPage"/>

内容设置如下:

public class ContentSettings extends AppCompatActivity {


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

    }


}

我正在尝试从 ContentProfile 中的按钮转到 ContentSettings。我已阅读此here 的文档。我试图关闭这个文档,但我似乎无法弄清楚。我应该查看另一个文件来解决这个问题吗?我忽略了一些简单的事情吗?

编辑:这是我的 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user.speed_read">

    <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".LoginActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ContentProfile"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="com.example.user.speed_read.ContentSettings" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ContentSettings"></activity>
    </application>

</manifest>

【问题讨论】:

    标签: android onclick buttonclick


    【解决方案1】:

    如果你想在点击按钮时传递另一个活动,你可以使用按钮点击监听器。简单的方法来做到这一点。如果你这样做,你可以从你的 xml 中删除 onclick。

    final Button button = (Button) findViewById(R.id.button11);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 Intent intent = new Intent(ContentProfile.this, ContentSettings.class);
                 startActivity(intent);
             }
         });
    

    【讨论】:

    • 我已经尝试过这个解决方案,但当我点击按钮时仍然没有任何反应。真的很困惑。
    【解决方案2】:
    1. 内容配置文件类中的 onClickListener 方法在哪里?您的 onClickListener 方法必须在其中包含 settingsPage 方法。示例:

    final Button settingsButton = (Button) findViewById(R.id.settingsButton);

    View.OnClickListener goToSettings = new View.OnClickListener(){
           @Override
           public void onClick(View view) {
                openSettings();
           }
    };
    

    settingsButton.setOnClickListener(goToSettings);

    1. 您必须在 AndroidManifest.xml 中指定 ContentSettings 活动

    2. ContentSettings 活动必须为 XML 布局设置正确的内容名称。 activity_content_settings 是您的 XML 布局的确切名称吗?

    【讨论】:

    • openSettings() 在做什么?那只是我的 settingsPage() 方法吗?不知道你是如何打破这一切的。 View.OnClickListener 是否在 onCreate 中?
    • 第 2 点和第 3 点也是肯定的。它在 AndroidManifest 和完全相同的 XML 中
    • openSettings 只是您的 settingsPage 方法,是的。这只是我在自己的一个应用程序中调用设置页面方法的名称。但这是一回事。
    • 还有 Button 声明、View.OnClickListener 和 settingsButton.setOnClickListener(goToSettings) 都进入 onCreate 方法
    猜你喜欢
    • 2019-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 1970-01-01
    • 2016-08-14
    • 1970-01-01
    相关资源
    最近更新 更多