我肯定错过了一些东西,所以我需要一双额外的眼睛!
我有一个按钮,当点击时我希望NewConnection
Activity
出现。
我已在AndroidManifest.xml中声明了所有活动,但它不起作用。
MainActivity :
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the user interface layout for this Activity
setContentView(R.layout.activity_main);
}
//button addButton take you to NewConnection Activity
public void newConnection(View view) {
Intent intent = new Intent(MainActivity.this, NewConnection.class);
startActivity(intent);
}
}
NewConnection
public class NewConnection extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the user interface layout for this Activity
setContentView(R.layout.activity_new_connection);
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tuwien.process_monitoring"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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=".NewConnection"
android:label="@string/app_name">
</activity>
</application>
</manifest>
答案 0 :(得分:1)
您应该定义按钮并在其上设置监听器。假设您的Button在R.layout.activity_main
中定义并且id等于“btn”:
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent(MainActivity.this, NewConnection.class);
startActivity(intent);
}
});
Piotr Golinski是对的。您可以使用属性android:onClick
指定要在布局文件中调用的方法。例如:
<Button android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="newConnection" />