Java 添加更多按钮
我正在开发一个应用程序。过去的日子里,我试图在我的页面上找到更多的按钮。但我想不出来。有人能帮忙吗?我对发展一无所知:) 布局(xml) 主要活动Java 添加更多按钮,java,android,button,Java,Android,Button,我正在开发一个应用程序。过去的日子里,我试图在我的页面上找到更多的按钮。但我想不出来。有人能帮忙吗?我对发展一无所知:) 布局(xml) 主要活动 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" and
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
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="com.rust.rustapp.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button"
android:onClick="button1Click"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="38dp"
android:text="Button"
android:onClick="button1Click"
/>
</RelativeLayout>
Intro.xml(按钮1正在打开此页面)
舱单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rust.rustapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
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=".Intro"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
新的布局名称是:FunPage
谢谢 从您为
MainActivity
粘贴的代码来看,您似乎混合了两种不同的方式来处理按钮单击
如果可以从布局xml注册click侦听器,则与onClick属性相对应的方法需要是公共的,而不是在按钮1
侦听器中声明为方法
另一种方法是只使用Java代码侦听单击事件。如果这样做,则从该按钮的布局XML中删除onClick属性。有很多很多例子
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
//TODO -- In your case, start the "Intro" activity
}
}
如果您将该方法提取出来以启动指向click listener之外的方法的Intro活动(即作为MainActivity的方法),可能会有所帮助
package com.rust.rustapp;
import android.app.Activity;
import android.os.Bundle;
public class Intro extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.intro);
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rust.rustapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
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=".Intro"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
//TODO -- In your case, start the "Intro" activity
}
}