简体   繁体   English

显示在xml中选择的图像并在第二次启动时更改main_activity

[英]display image choosen in xml and change main_activity on second time launch

i want to learn how to create an android app, and i'm confused at : 我想学习如何创建一个android应用程序,我感到困惑:

1.first time launching my app will display viewpagers img_btn ice, img_btn jelly. 1.第一次启动我的应用程序时,将显示viewpagers img_btn ice,img_btn jelly。 and when img_buttons clicked will display which image chosen in profil.xml , is there any solution do it in 1 xml or i must create 2 xml for each button? 当单击img_buttons时 ,将显示在profil.xml中选择的图像,请问有没有解决的办法以1 xml或我必须为每个按钮创建2 xml?

2.if user have choosed img_button, main_activity will change to profil.xml, so for the second time launch will show profil.xml. 2.如果用户选择了img_button,main_activity将更改为profil.xml,因此第二次启动将显示profil.xml。 how can i do that? 我怎样才能做到这一点?

main_activity.xml main_activity.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#000000"
>
<TextView
    android:text="Choose char"
    android:textSize="25dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#1e00ff"/>
<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_ViewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"/>

page1.xml page1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Halaman 2"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/textView"
    android:layout_gravity="center_horizontal"
    android:textSize="54dp"
    android:textColor="#0aedff"/>
<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageButton"
    android:background="#00000000"
    android:src="@drawable/btn_info"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center_vertical|center"/>

page1.java page1.java

import android.support.v4.app.*;
import android.view.*;
import android.os.*;

public class page1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,   Bundle savedInstanceState)
{
    View v=inflater.inflate(R.layout.halaman_1, container,false);
    return v;
}
}

page2.xml and page2.java same as page1 English isn't my first language, so please excuse any mistakes. 与page1相同的page2.xml和page2.java英语不是我的母语,因此请原谅任何错误。

I am not sure what Fragment you extended. 我不确定您扩展了什么片段。 import android.support.v4.app.Fragment; 导入android.support.v4.app.Fragment; or import android.app.Fragment; 或导入android.app.Fragment; The below example assume you use support.v4 以下示例假定您使用support.v4

public void replaceFragment(Fragment fragment, String title) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame, fragment)
                .addToBackStack(title)
                .commit();
    }

    public void addFragment(Fragment fragment, String title) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.content_frame, fragment, title)
                .addToBackStack(title)
                .commit();
    }

you better has 2 xml (eg. page1.xml, page2.xml) and two classes extend Fragment, you may use the above function to first add a fragment, subsequently use replace when you open new fragments. 您最好有2个xml(例如page1.xml,page2.xml)和两个扩展Fragment的类,您可以使用上面的函数先添加一个片段,然后在打开新片段时使用replace。 Notice there is a R.id.content_frame in the functions given. 注意,给定的函数中有一个R.id.content_frame。 That is from your main_activity.xlm, make a linear_layout name it content_frame, which will hold the open fragment. 那是从您的main_activity.xlm中,将一个linear_layout名称命名为content_frame,它将保存打开的片段。 You then put a button on page1.xml, put an ImageView inside page2.xml, when the button on page1 has been clicked. 然后,当单击page1上的按钮时,将一个按钮放在page1.xml上,将ImageView放在page2.xml内。 use replaceFragment to open page2.xml 使用replaceFragment打开page2.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每次加载Main_activity时调用特定函数吗? - Call a specific function every time Main_activity is loaded? Eclipse没有找到Main_Activity - Eclipse not finding Main_Activity main_activity java文件的错误消息 - error messages with main_activity java file 如果main_activity已经实现ActionBar.TabListener,如何在main_activity中的另一个类中实现接口 - How to implement an Interface in another class in main_activity if main_activity already implements ActionBar.TabListener 使用main_activity中的extends Activity在Actionbar中添加徽标 - Add Logo in Actionbar using extends Activity in the main_activity 如何从 Helper 类调用 Main_Activity 类中的方法? - How to call the method in Main_Activity Class from Helper class? 如何从android中的普通类调用main_activity的方法 - how to call a method of a main_activity from a normal class in android 试图用另一个片段onbuttonclick替换main_activity片段 - Trying to replace main_activity fragment with another fragment onbuttonclick 使userInput从主要活动显示在Android的第二个活动中 - Making userInput from main activity to display on second activity in Android 使用主活动中的按钮更改第二个活动中的颜色 = 崩溃 - Change color in second activity using button in main activity = crashes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM