gpt4 book ai didi

java - 单击按钮无法启动另一个 Activity

转载 作者:行者123 更新时间:2023-11-29 21:39:57 24 4
gpt4 key购买 nike

我的项目非常简单,仅供培训之用。当我单击主布局上的按钮时,我试图加载另一个界面/ View /Activity 。这是主要 Activity 的代码:

包 com.example.interfacemanipulation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

public static final String EXTRA_MESSAGE = "Something Here....";

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


@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;
}

public void goToWelcomeActivityMethod(View view)
{
Intent intent = new Intent(this, WelcomeActivity.class);
EditText editText = (EditText) findViewById(R.id.welcomeMessage);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}

}

这是另一个 Activity 中的代码:

package com.example.interfacemanipulation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class WelcomeActivity extends Activity {

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

Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);

// Set the text view as the activity layout
setContentView(textView);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.welcome, menu);
return true;
}

}

这里的代码基本上是根据谷歌文档修改的。我只想能够加载带有标签的第二个 Activity 的 View 。这是我的 list :

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

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.interfacemanipulation.MainActivity"
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="com.example.interfacemanipulation.WelcomeActivity"
android:label="@string/title_activity_welcome" >
</activity>
</application>

</manifest>

过去 4 天我一直在尝试使用 android...所以感谢您的支持

最佳答案

你有 2 个问题:1. 你没有带你去 Activity 的按钮(或者至少你没有在你发布的代码中显示它。如果你有,那么你没有onClickListener 为该按钮设置)。 2. 您没有调用将您带到第二个 Activity 的方法。

要有一个将您带到另一个 Activity 的按钮,您需要将按钮添加到布局,然后添加 onClickListener,它会在您执行时执行您的 goToWelcomeActivityMethod点击它,像这样:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

public static final String EXTRA_MESSAGE = "Something Here....";

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

//this will insert the button into the Main Activity layout
Button goToWelcome = (Button)findViewById(R.id.goToWelcome);

//this is the onClickListener which will call the method to go to the next activity
goToWelcome.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
goToWelcomeActivityMethod();
}
});
}


@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;
}
//you don't need to pass in the View
public void goToWelcomeActivityMethod()
{
Intent intent = new Intent(this, WelcomeActivity.class);
EditText editText = (EditText) findViewById(R.id.welcomeMessage);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}

}

然后在您的 XML 文件中确保您也添加了按钮:

 <Button
android:id="@+id/goToWelcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Go To Welcome" />

关于java - 单击按钮无法启动另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17454743/

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