java不在同一项目中点击按钮切换帐户
这是一个关于布局的应用 首先,屏幕将在clciking按钮上处于纵向布局,它将更改为横向布局。所以在这里点击按钮,它不会进入下一个活动,尽管没有发现编译错误
主要活动。xml
?xml version="1.0" encoding="utf-8"?>
<安卓.support.constraint.ConstraintLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context="com.example.vamshivikas.orientation.MainActivity">
<TextView
安卓:id="@+id/textView"
安卓:layout_width="wrap_content"
安卓:layout_height="28dp"
安卓:text="@string/this_is_potrait_layout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.48"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.15" />
<Button
安卓:id="@+id/button1"
安卓:layout_width="wrap_content"
安卓:layout_height="47dp"
安卓:layout_marginStart="8dp"
安卓:layout_marginTop="80dp"
安卓:layout_marginEnd="8dp"
安卓:layout_marginBottom="8dp"
安卓:text="@string/go_to_next_activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</安卓.support.constraint.ConstraintLayout>
活动_秒。xml
<?xml version="1.0" encoding="utf-8"?>
<安卓.support.constraint.ConstraintLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<TextView
安卓:id="@+id/textView2"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginStart="8dp"
安卓:layout_marginTop="8dp"
安卓:layout_marginEnd="8dp"
安卓:layout_marginBottom="8dp"
安卓:text="@string/this_is_landscape_layout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
活动艺术节。xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
package="com.example.vamshivikas.orientation">
<application
安卓:allowBackup="true"
安卓:icon="@mipmap/ic_launcher"
安卓:label="@string/app_name"
安卓:roundIcon="@mipmap/ic_launcher_round"
安卓:supportsRtl="true"
安卓:theme="@style/AppTheme">
<activity
安卓:name="com.example.vamshivikas.orientation.MainActivity"
安卓:screenOrientation="portrait">
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity 安卓:name=".SecondActivity"
安卓:screenOrientation="landscape">
</activity>
</application>
</manifest>
主要活动。java
package com.example.vamshivikas.orientation;
import 安卓.content.Intent;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
}
public void onClcik(View v){
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
第二个活动。java
package com.example.vamshivikas.orientation;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
public class SecondActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
# 1 楼答案
如果我能很好地理解你,当你点击按钮时,你想去另一个活动吗?你还想切换到景观吗
您可以这样做:(在onClick()函数中)
Intent intent = new Intent(this, AnotherActivity.class); startActivity(intent);
然后在第二个活动中,在onCreate()函数调用中:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
# 2 楼答案
正如我看到的,在activity_main中的Button元素中没有onClick属性。xml。 给你的按钮一个像这样的onClick属性
然后在MainActivity中创建一个public void方法。使用相同名称的java,并在其中处理您的内容。像这样:
并考虑使用MRFISH MANS溶液