单击按钮并打开新活动

时间:2014-04-15 22:42:08

标签: android eclipse

这可能被视为一个愚蠢的问题,但我对Android很新。

我正在创建一个应用程序并且我添加了一个信息活动,我的主页上有一个信息按钮,并且当我点击它时想要转到信息活动.. 我实际上不确定该怎么做。

这是Button的xml:

<Button
    android:id="@+id/button2"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:background="#FFFFFF"
    android:text="@string/info_btn" />

我是否在主要活动,介绍活动或xml中添加代码? 这是我的主要活动

package ie.gmit.project;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;

public class MainActivity extends ActionBarActivity  {

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}

public void openSite(View v){
    Uri uri = Uri.parse("http://gmitsu.ie/clubssocs/directory.html");
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}

}

更新:  这有更新的代码,并且运行无错误...但是当我运行它时按钮仍然不会转到新的活动。

package ie.gmit.project;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;

public class MainActivity extends ActionBarActivity  {


private Button button2;//this is a bad name

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

    button2= (Button) findViewById(R.id.button2);//find the button


    button2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(v.getContext(), IntroActivity.class);
            startActivity(i);
           // finish();//if you want to close main activity after start info activity
    }});

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }


}

public void openSite(View v){
    Uri uri = Uri.parse("http://gmitsu.ie/clubssocs/directory.html");
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}

}

清单

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="ie.gmit.project.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="ie.gmit.project.IntroActivity"
            android:label="@string/title_activity_intro" >
        </activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:2)

试试这个

public class MainActivity extends Activity {

private Button button2;//this is a bad name

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

    button2= (Button) findViewById(R.id.button2);//find the button


    button2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(v.getContext(), InfoActivity.class);
            startActivity(i);
            finish();//if you want to close main activity after start info activity
    });
}

在清单

中的清单介绍活动中声明
    <activity
        android:name=".IntroActivity"
        android:label="Intro Activity"
        android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" >
    </activity>

如果这不起作用,您可以在日志中添加条目以查看数据是否分配良好,例如:

        @Override
        public void onClick(View v) {
            Log.i("MyLog", "Im here!");
            Intent i = new Intent(v.getContext(), InfoActivity.class);
            startActivity(i);
            finish();//if you want to close main activity after start info activity
    });