gpt4 book ai didi

java - 单击按钮时更改 xml Activity

转载 作者:行者123 更新时间:2023-12-01 06:10:51 25 4
gpt4 key购买 nike

这是我的主要 Activity ,我遇到了 customer_layout.class 和 main_menu.class 抛出错误的问题。它一直说“找不到符号类 customer_layout 或“找不到符号类 main_menu”

这是屏幕截图

enter image description here

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
Button CJB;




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//CJB = findViewById(R.id.button1);
// CJB.setOnClickListener(this);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
public void customerPage(View v){
Intent customerPage = new Intent(MainActivity.this, Customer_Layout.class);
startActivity(customerPage);
}
public void loginPage(View v){
Intent loginPage = new Intent(MainActivity.this, Main_Menu.class);
startActivity(loginPage);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.andy.androidautoprototype.MainActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:onClick="loginPage"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

最佳答案

Main_Menu.xml 和 Customer_layout.xml 不是类,它们是 XML 文件。您需要创建相应的 Activity 来使用这些 XML 文件作为内容 View 。

public class MainMenuActivity extends Activity {}
public class CustomerLayoutActivity extends Activity {}

您可以使用 fragment ,但我对您的应用程序 UI 层次结构了解不够,无法进行该调用。如果您确实沿着这条路走下去,您将为您想要使用的每个布局创建一个 Fragment 类。

public class MainMenuFragment extends Fragment {}
public class CustomerLayoutFragment extends Fragment {}

然后在您使用的 fragment 类的 onCreateView() 回调中膨胀这些布局 Main_Menu.xmlCustomer_Layout.xml

完成后,在 activity_main.xml 中添加一个 fragment 容器,如下所示:

<FrameLayout
android:id="@+id/fragment_container">

</FrameLayout>

在您的 Activity 类中,使用 FragmentManager 实例化您的 Fragment 类,并将其添加到 FragmentTransactioncommit() 事务中它将被添加到您的 fragment 容器中。

查看 Android Fragment如果您决定走这条路,请参阅文档进行更深入的了解。

关于java - 单击按钮时更改 xml Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35121510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com