5

我有 2 个文件:main_activity.xml 和 home.xml。我在 main_activity.xml 中做了一个按钮

这是代码片段:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:background="@drawable/splash_background"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context=".MainActivity" >

<Button
    android:id="@+id/Home"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="43dp"
    android:onClick="home"
    android:text="Home" />

</RelativeLayout>

然后,我有了 home.xml。我希望按钮打开 home.xml。我怎样才能做到这一点?我不知道任何java,而且我是android开发的新手。

这是我的 home.xml 下面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@drawable/app_bg"
    android:layout_height="match_parent"
    android:orientation="vertical" >


</LinearLayout>

下面是我的 AndroidManifest.xml:

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

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

 <application
    android:allowBackup="false"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.idozer.SplashActivity"
        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.idozer.MainActivity"
        android:label="@string/app_name" >
    </activity>
</application>
</manifest>

这就是我所拥有的。如果您回复,请告诉我在哪里添加代码,例如目录或代码片段之间。

4

6 回答 6

8

要在 android 中管理点击活动,您可以执行以下操作

  1. OnClickListenerYourActivity.java类上实现,如

    public class MainActivity extends Activity implements OnClickListener

  2. 然后,在 .java 类中声明您的按钮,例如

    Button btn = (Button) findViewById(R.id.btnPlay);

  3. 然后使用按钮btn变量如下

    btn.setOnClickListener(new View.OnClickListener() {
    
        public void onClick(View v) {
            myClick(v); /* my method to call new intent or activity */
        }
    });
    
  4. 处理点击事件:

    public void myClick(View v) {
        Intent intent = new Intent(**this, Swipe.class**);
        startActivity(intent);// for calling the activity
    }
    

您还需要在android manifest下面注册您的活动(.java)

<activity
    android:name=".Swipe"
    android:screenOrientation="landscape" >
</activity>
于 2013-03-13T04:27:38.867 回答
2

您可以使用此代码。

安卓:OnClickListener

在我们的活动类中,我们添加了 onclick 方法。

在我们的活动类中,我们添加了 onclick 方法。

    //On click event for button1
public void button1OnClick(View v) {
    //Inform the user the button has been clicked
    Toast.makeText(this, "Button1 clicked.", Toast.LENGTH_SHORT).show();               
}

在布局文件中,我们添加对活动中 onclick 处理程序的引用。应用程序会自动将 onclick 方法绑定到视图(在本例中为 button1)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button1"
            android:onClick="button1OnClick"/>
</LinearLayout>
于 2015-02-21T05:09:26.343 回答
1

创建另一个类转到您的项目右键单击并单击类并创建主页。在那个 Home 类文件中扩展活动并添加这样的代码

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

启动活动类中添加这一行

Intent intent = new Intent(SplashActivity.this,Home.class);
startActivity(intent);

在您的 android 清单文件中添加 Home 活动类

<activity android:name="com.example.idozer.Home"
    android:label="@string/app_name" >
</activity>
于 2013-03-13T04:29:31.840 回答
0

I will give you just a little bit to get you started as this answer may help others that are having trouble with using onClick() this way but you really need to learn Java and go through the Android Docs so you can ask better questions

You need to read Here about Actviities and how to create them. Then in your code you will have a function

 public void home(View v)  //the name of this function comes from where you declared in your manifest `android:onClick="home"
{
     Intent intent (MainActivity.this, HomeActivity.class); //MainActivity is the name of current activity and HomeActivity is the name of the activity you want to start
     can add intent extras/flags/categories here
     startActivity(intent);
}

You also need to add the HomeActivity in your manifest as you have for the other Activities.

But you really need to go through the docs and do some tutorials to get an idea of how the Android framework operates and you need to learn Java to make your life a whole lot easier. Besides the previous two links I have given, also see this post about click events as there are different ways to use onClick()

I hope this is enough to get you started and I really hope you go through the docs to get a better understanding of what you are doing. Good luck to you!

Another important link to get started

Intents

于 2013-03-13T04:38:54.430 回答
0

android:onClick在 API 级别 4 中添加以使其更容易、更类似于 Javascript 网络,并从 XML 驱动一切。它在内部做的是在OnClickListener上添加一个Button,它调用你的 home 方法。

<Button
  android:id="@+id/Home"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentRight="true"
  android:layout_alignParentTop="true"
  android:layout_marginRight="43dp"
  android:onClick="home"
  android:text="Home" />

.

public void home(View view){
  Intent intent=new Intent (view.getContext(),Luton.class);
  this.startActivity(intent);
}

在你的活动课上

使用 java 代码,您可以通过从 xml 获取按钮的 id 来单击按钮。

<Button
  android:id="@+id/myHomeButton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentRight="true"
  android:layout_alignParentTop="true"
  android:layout_marginRight="43dp"
  android:text="Home" />

.

Button button= (Button) findViewById(R.id.myHomeButton);
button.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
     //do whatever you want on button click
  }
});   

两者完全相同

于 2013-03-13T04:35:52.797 回答
0
Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
        }
    });
于 2018-04-13T16:50:53.240 回答