[英]open new activity with button
here is the code of my xml. 这是我的xml的代码。
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn2"
android:layout_gravity="center_horizontal"
android:text="Make New Account"
android:onClick="new"
android:textColor="#E74C3C"
android:textStyle="bold"
android:paddingTop="20dp"/>
and here is my code of java. 这是我的Java代码。
public Button btn2;
public void onClick(){
btn2=(Button)findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,submit.class);
startActivity(intent);
}
});
}
my button is not working yet, even an message will appear after clicking on button that unfortunately application has stopped... what is the reason?? 我的按钮仍无法正常工作,单击按钮后,甚至会出现一条消息,不幸的是应用程序已停止……原因是什么?
Try this code:- 试试这个代码:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn2"
android:layout_gravity="center_horizontal"
android:text="Make New Account"
android:textColor="#E74C3C"
android:textStyle="bold"
android:paddingTop="20dp"/>
Put this in your activity:- 把它放在你的活动中:
btn2 = (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this,submit.class);
startActivity(intent);
}
});
您的代码是完美的奈达。只需在清单文件中添加您的活动名称,如下面的代码,
<activity android:name=".submit"/>
You have two ways: 您有两种方法:
Either 要么
1) Set an onClick listener on the button 1)在按钮上设置一个onClick侦听器
Or 要么
2) Set an onClick attribute on the button and create a method 2)在按钮上设置onClick属性并创建方法
Method 1 方法1
Xml file xml文件
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn2"
android:layout_gravity="center_horizontal"
android:text="Make New Account"
android:textColor="#E74C3C"
android:textStyle="bold"
android:paddingTop="20dp"/>
Java File Java文件
public Button btn2;
btn2 = (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this,submit.class);
startActivity(intent);
}
});
Method 2 方法2
Xml file xml文件
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn2"
android:layout_gravity="center_horizontal"
android:onclick="newAccount"
android:text="Make New Account"
android:textColor="#E74C3C"
android:textStyle="bold"
android:paddingTop="20dp"/>
Java file Java文件
public Button btn2;
btn2=(Button)findViewById(R.id.btn2);
public void newAccount(View v) {
Intent intent = new Intent(MainActivity.this,submit.class);
startActivity(intent);
}
The thing is you are calling the onClick function when you have declared the onClick of button as new 问题是当您将按钮的onClick声明为new时,您正在调用onClick函数
Try this : 尝试这个 :
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn2"
android:layout_gravity="center_horizontal"
android:text="Make New Account"
android:onClick="submit"
android:textColor="#E74C3C"
android:textStyle="bold"
android:paddingTop="20dp"/>
And then in your activity class declare this function: 然后在您的活动类中声明此函数:
public void submit(View view){
Intent intent = new Intent(MainActivity.this,submit.class);
startActivity(intent);
}
That's it. 而已。 Hope this helps.
希望这可以帮助。
Activity
is the most basic Android components is also the most common use of the four components ( Activity
, Service
, Content Provider
, BroadcastReceiver
). Activity
是最基本的Android组件,也是四个组件( Activity
, Service
, Content Provider
, BroadcastReceiver
)最常用的用法。
The steps to create an Activity
: 创建
Activity
的步骤:
Create a new Java class And extends the Activity
创建一个新的Java类并扩展
Activity
Add in the AndroidManifest
添加
AndroidManifest
<activity android:name=".ActivityClassName"/>
If is to start the interface 如果是启动界面
<activity android:name=".ActivityClassName"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity>
Override the onCreate()
function and load layout 覆盖
onCreate()
函数并加载布局
Note: The Activity of the Java classes generally ends in Activity
注意: Java类的活动通常以
Activity
结尾
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.