Android 安卓按钮onClick未转换为新活动
因此,我对Android开发非常陌生,这可能是一个简单的问题,我错过了一件简单的事情。我有一个类,我有一个按钮,我试图点击一个按钮,并过渡到另一个活动Android 安卓按钮onClick未转换为新活动,android,onclick,buttonclick,Android,Onclick,Buttonclick,因此,我对Android开发非常陌生,这可能是一个简单的问题,我错过了一件简单的事情。我有一个类,我有一个按钮,我试图点击一个按钮,并过渡到另一个活动 public class ContentProfile extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
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);
}
}
我正在尝试开设内容设置课程。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"/>
我正在尝试从ContentProfile中的按钮转到ContentSettings。我已经阅读了这方面的文档。我一直在试图摆脱这个文档,但我似乎无法理解它。我应该看另一份文件来解决这个问题吗?我是否忽略了一些简单的事情
编辑:这是我的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>
如果要在单击按钮时传递另一个活动,可以使用按钮单击侦听器。简单的方法。如果这样做,可以从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);
}
});
最终按钮设置按钮=(按钮)findViewById(R.id.settingsButton)代码>
settingsButton.setOnClickListener(goToSettings)代码>
必须在AndroidManifest.xml中指定ContentSettings活动
<?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>
ContentSettings活动必须为XML布局设置正确的内容名称。活动内容设置是否为XML布局的确切名称
openSettings()在做什么?这就是我的setingspage()方法吗?不知道你是怎么把这一切打破的。View.OnClickListener是否进入onCreate?第2点和第3点都是肯定的。它在AndroidManifest中,完全相同的XMLopenSettings只是您的settingsPage方法,是的。这正是我在自己的一个应用程序中调用设置页面方法的名称。但是这是一样的。另外,按钮declation、View.OnClickListener和settingsButton.setOnClickListener(goToSettings)都包含在onCreate方法中。我尝试了这个解决方案,但当我单击按钮时,仍然没有发生任何事情。真是莫名其妙。
View.OnClickListener goToSettings = new View.OnClickListener(){
@Override
public void onClick(View view) {
openSettings();
}
};