单击按钮时不会打开新意图

时间:2011-11-15 16:35:43

标签: android

  

块引用

我只需点击按钮即可尝试更改显示。我想我应该使用Intent做这个但没有任何影响,请帮助我:( 这是我的代码:

FYP.JAVA

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button ProfileTemplate = (Button)findViewById(R.id.ProfileTemplate);
    ProfileTemplate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent mIntent = new Intent(FYP.this, ProfileTemplate.class);
            startActivity(mIntent);
            finish();
        }
    });



public class ProfileTemplate extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profiletemplate);
    }
}

1 个答案:

答案 0 :(得分:0)

以下是包含2项活动的完整项目。一个有按钮,当你点击它时,它会启动另一个Activity。

更多关于Buttons and Clicking

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="Hello, sup son!" />

    <Button android:id="@+id/btnButton" android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:layout_gravity="left"
        android:onClick="changeActivity" android:text="changeActivity" />
</LinearLayout>

main2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="New Activity Started" />

</LinearLayout>

类Hello.java

 public class Hello extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

    //default button handler
public void changeActivity(View view) { 
    startActivity(new Intent(this, NewActivity.class));
}
}

类NewActivity.java

public class NewActivity  extends Activity { 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);

    } 

}

的Manifest.xml

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

    <application android:icon="@drawable/icon" android:label="New App Name">
        <activity android:name=".Hello"
                  android:label="New App Name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".NewActivity" android:label="New App Name"></activity>

    </application>
</manifest>