我想知道你是否可以使用按钮点击将用户发送到另一个.xml布局而没有使用Intent intent = new Intent(this,layout_name.xml)的第二个.java类;?
或者我必须创建另一个.java类,以便我可以使用Intent。
我使用了这个guide,但它似乎没有锻炼
MainActivity
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/*
This app is for a local Vape Shop
it's purpose is to increase customer flow, and provide a menu for Android users
the array is going to be called = da_menu
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateListView();
}
public void sendMessage(View view) {
// once this button is clicked it wil send the user to the about us activity
Intent intent = new Intent(this, aboutus.xml );
}
private void populateListView() {
// Create the list of items
String[] juiceMenu= {
"#1 BlueBerry Bombshell",
"#2 Richie Rich",
"#3 Chiquita",
"#4 Afternoon Delight",
"#5 Poppin Otters",
"#6 Viva La Sangria",
"#7 Okole Maluna",
"#8 Carmen Miranda",
"#9 Pomalade",
"#10 Izual",
"#11 Butter Stotch",
"#12 Blue Bulls",
"#13 Grape Ape",
"#14 Bruce Juice",
"#15 Doc Holiday",
"#16 Peachy Keen",
"#17 Hula",
"#18 New York",
"#19 Al Gore",
"#20 Lux Charms",
"#21 Sailor Jack",
"#22 Get Him to the Greek",
"#23 Key We Lie Chi",
"#24 Spring Fling",
"#25 Gumby",
"#26 Chai-Milk",
"#27 Mr. Bean",
"#28 50 Shades of Orange",
"#29 Blue Waffles",
"#30 Enigma",
"#31 Mr. Freeze",
"#32 Cup of Joe"};
// Build adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, // context for the activity
R.layout.da_menu, // Layout to create
juiceMenu); // Items to be displayed
//configure list view
ListView list = (ListView) findViewById(R.id.listViewMain);
list.setAdapter(adapter);
}
}
activity_main.xml中 这是主页面,我想要点击的按钮用android:id =“@ + id / buttonAboutUs”声明
<?xml version="1.0" encoding="utf-8"?>
<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.example.vitoriano_vaz.eastbayvapes.MainActivity">
<TextView
android:textAllCaps="true"
android:textSize="22dp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to East Bay Vapes"
android:id="@+id/textView" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listViewMain"
android:layout_centerHorizontal="true"
android:layout_below="@+id/buttonAboutUs" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About Us & Hours of Operation"
android:id="@+id/buttonAboutUs"
android:onClick="sendMessage"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true"
android:layout_alignEnd="@+id/textView" />
</RelativeLayout>
aboutus.xml 这是我希望在点击按钮后发送ser的屏幕
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="About Us"
android:id="@+id/AboutUs"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/editText"
android:text="We are dedicated to helping people get off tobacco! We carry everything from beginner setups, to advanced personalized vaporizers, as well as premium juices all made in house! "
android:layout_below="@+id/AboutUs"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:phoneNumber="false"
android:textStyle="bold" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/editText2"
android:layout_below="@+id/editText"
android:text="eastbay@gmail.com"
android:textStyle="bold"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:ems="10"
android:id="@+id/editText3"
android:layout_below="@+id/editText2"
android:text="1111 N RD Live, Ca "
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:textStyle="bold" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/editText4"
android:layout_below="@+id/editText3"
android:text="555-555-5555"
android:textStyle="bold"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText5"
android:layout_below="@+id/editText4"
android:text="facebook.com/eastbay"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:textStyle="bold" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText6"
android:layout_below="@+id/editText5"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:text="Instagram: @eastbay"
android:textStyle="bold" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/editText7"
android:layout_below="@+id/editText6"
android:layout_alignParentBottom="true"
android:text="Monday - Saturday 10:00 am - 8:00 pm\n Sunday 10:00 am - 6:00 pm"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
</
答案 0 :(得分:0)
意图用于开始活动,服务或提供广播。相反,您应该创建两个片段并使用片段事务来替换另一个片段。您可能必须使用FrameLayout
创建占位符,然后将第一个片段添加到其中。然后使用FragmentTransaction
替换它。
编辑以进行详细说明。
您仍然会有两个描述所需布局的xml文件。但是你也会有一个FrameLayout的第三个。使用其布局文件来设置setContentView。不要太在意它,因为你要立即动态替换它。
创建两个片段 - 这些是将扩展片段的Java类,以及您将为每个布局充气的位置。
public class FirstFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout_one = inflater.inflate(R.layout.layout_one, container, false);
return userIconFragView;
}
}
然后使用onCreate显示第一个片段。这意味着,在您的主要活动中,您可以创建对FirstFragment ff = new FirstFragment();
显示它:
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.my_frame_layout_id, ff).commit();
在触发交换的方法中:
SecondFragment sf = new SecondFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.my_frame_layout_id, sf); //1st param might have to be the view group id that contains your FrameLayout, you should double check this, I can’t remember.
transaction.commit();
答案 1 :(得分:0)
查看ViewSwitcher类。有不同的方法可以做到这一点,但我会这样做:
在您的主要活动中,像对待任何其他视图一样获取对您的viewswitcher的引用,夸大您的第一个和第二个视图,并将它们添加到viewswitcher。
viewSwitcher =(ViewSwitcher)view.findViewById(R.id.viewSwitcher); 查看firstLayout = inflater.inflate(R.layout.firstLayout,null); 查看secondLayout = inflater.inflate(R.layout.secondLayout,null); viewSwitcher.addView(firstLayout); viewSwitcher.addView(secondLayout);
正常开展业务,直到您想向用户显示下一个视图;此时调用:viewSwitcher.showNext();
我希望这会帮助你。一个很好的示例教程(以与我描述的方式略有不同的方式)可以在这里找到:http://android-er.blogspot.co.uk/2013/11/example-of-viewswitcher.html
Android文档供参考:http://developer.android.com/reference/android/widget/ViewSwitcher.html
答案 2 :(得分:0)
您可能希望查看本教程 http://developer.android.com/training/basics/fragments/index.html 我使用它来制作一些设置菜单,其中我在顶部有一些按钮并在片段中切换不同的内容/布局。