如何在Android中创建动态滑动屏幕,并显示下面给出的布局

如何在Android中创建动态滑动屏幕,并显示下面给出的布局,android,Android,我有一个布局,其中有一个动态生成的单选按钮和一个提交按钮,该按钮在提交按钮上获取所选单选按钮的值。现在我想为(n)个页面创建一个动态生成的滑动视图,在这里我可以为不同的页面显示相同的布局。下面是我的方法和工作代码。提前谢谢 MainActivity.java public class MainActivity extends Activity { private String[] application = { "ABC", "DEF", "GHI", "HIJ" };

我有一个布局,其中有一个动态生成的单选按钮和一个提交按钮,该按钮在提交按钮上获取所选单选按钮的值。现在我想为(n)个页面创建一个动态生成的滑动视图,在这里我可以为不同的页面显示相同的布局。下面是我的方法和工作代码。提前谢谢

MainActivity.java

public class MainActivity extends Activity {

    private String[] application = { "ABC", "DEF", "GHI", "HIJ" };                     
    private String[] device = { "ABC", "DEF", "GHI", "HIJ" }; 
    private RadioGroup radioGroup1;
    private RadioGroup radioGroup2;
    private RadioButton btn;
    private RadioButton btn2;
    private String text1;
    private String text2;
    RadioButton button1;
    RadioButton button2;
    Button selectall;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        checkButtonClick();
        drawRadiobuttons();

    }

    private void checkButtonClick() {

        Button myButton = (Button) findViewById(R.id.findSelected);
        myButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                StringBuffer responseText = new StringBuffer();
                responseText.append("");

                // Get selected radiobuttons
                if (radioGroup1.getCheckedRadioButtonId() != -1) {
                    text1 = btn.getText().toString();
                    Log.d("Button", "Text 1 : " + text1);
                }

                if (radioGroup2.getCheckedRadioButtonId() != -1) {
                    text2 = btn2.getText().toString();
                    Log.d("Button", "Text 2 : " + text2);
                }


                Toast.makeText(
                        getApplicationContext(),
                        "Data Posting : APPLICATION : "
                                + text1 + " \nDEVICE : " + text2,
                        Toast.LENGTH_LONG).show();


            }
        });

    }

    private void drawRadiobuttons(){

        radioGroup1 = (RadioGroup) findViewById(R.id.radio1);
        radioGroup2 = (RadioGroup) findViewById(R.id.radio2);

        ViewGroup hourButtonLayout = (ViewGroup) findViewById(R.id.radio1);
        for (int i = 0; i < application.length; i++) {
            button1 = new RadioButton(this);
            button1.setId(i);
            button1.setText(application[i]);
            hourButtonLayout.addView(button1);

            radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        public void onCheckedChanged(RadioGroup mRadioGroup2,
                                int checkedId2) {
                            for (int i = 0; i < mRadioGroup2.getChildCount(); i++) {
                                btn = (RadioButton) mRadioGroup2.getChildAt(i);
                                int t = mRadioGroup2.getId();
                                System.out.println(t);

                                if (btn.getId() == checkedId2) {
                                    text1 = btn.getText().toString();
                                    Toast.makeText(getApplicationContext(),
                                            "You selected : " + text1,
                                            Toast.LENGTH_SHORT).show();
                                    return;
                                }
                            }
                        }
                    });

        }

        ViewGroup hourButtonLayout2 = (ViewGroup) findViewById(R.id.radio2); 
        for (int i = 0; i < device.length; i++) {
            button2 = new RadioButton(this);
            button2.setId(i);
            button2.setText(device[i]);
            hourButtonLayout2.addView(button2);

            radioGroup2
                    .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        public void onCheckedChanged(RadioGroup mRadioGroup,
                                int checkedId) {
                            for (int i = 0; i < mRadioGroup.getChildCount(); i++) {
                                btn2 = (RadioButton) mRadioGroup.getChildAt(i);
                                int t = mRadioGroup.getId();
                                System.out.println(t);

                                if (btn2.getId() == checkedId) {
                                    text2 = btn2.getText().toString();
                                    Toast.makeText(getApplicationContext(),
                                            "You selected : " + text2,
                                            Toast.LENGTH_SHORT).show();
                                    return;
                                }
                            }
                        }
                    });

        }

    }

}
公共类MainActivity扩展活动{
私有字符串[]应用程序={“ABC”、“DEF”、“GHI”、“HIJ”};
私有字符串[]设备={“ABC”、“DEF”、“GHI”、“HIJ”};
私人放射组放射组1;
私人放射组放射组2;
专用无线按钮;
专用无线按钮btn2;
私有字符串text1;
私有字符串text2;
单选按钮1;
无线电按钮2;
按钮选择全部;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
选中按钮单击();
drawRadiobuttons();
}
私有无效检查按钮单击(){
按钮myButton=(按钮)findViewById(R.id.findSelected);
myButton.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
StringBuffer responseText=新的StringBuffer();
responseText.追加(“”);
//获取选定单选按钮
如果(RadioGroup 1.getCheckedRadioButtonId()!=-1){
text1=btn.getText().toString();
Log.d(“按钮”、“文本1:+text1”);
}
如果(RadioGroup 2.getCheckedRadioButtonId()!=-1){
text2=btn2.getText().toString();
Log.d(“按钮”、“文本2:+text2”);
}
Toast.makeText(
getApplicationContext(),
“数据过帐:应用程序:”
+text1+“\n设备:”+text2,
Toast.LENGTH_LONG).show();
}
});
}
专用单选按钮(){
放射组1=(放射组)findViewById(R.id.radio1);
放射组2=(放射组)findViewById(R.id.radio2);
ViewGroup hourButtonLayout=(ViewGroup)findViewById(R.id.radio1);
for(int i=0;i
活动\u main.xml

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textColor="#000"
        android:text="Select Question1"
        android:textSize="18sp"/>

    <Button
        android:id="@+id/findSelected"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit"/>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="200dip"
        android:orientation="vertical"
        android:scrollbars="none" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="200dip"
            android:orientation="vertical">

            <RadioGroup
                    android:id="@+id/radio1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dip"
                    android:layout_marginTop="5dip"
                    android:background="#fff"
                    android:checkedButton="@+id/sound" >
                </RadioGroup>
        </LinearLayout>
    </ScrollView>

     <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_marginTop="5dip"
        android:layout_marginBottom="5dip"
        android:text="Select Question2"
        android:textColor="#000"
        android:textSize="18sp"/>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="360dip"
        android:orientation="vertical"
        android:scrollbars="none" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="560dip"
            android:orientation="vertical">

            <RadioGroup
                    android:id="@+id/radio2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dip"
                    android:layout_marginTop="5dip"
                    android:background="#fff"
                    android:checkedButton="@+id/sound">
                </RadioGroup>

        </LinearLayout>
    </ScrollView>

</LinearLayout>


有人能帮忙吗?

如果我没记错的话,
滑动视图
是指
查看页面
。 您可以参数化
ViewPager
的适配器,即将页数传递给
适配器的构造函数。
查看
FragmentPagerAdapter
FragmentStatePagerAdapter

  • 在您的按钮中,单击listener计算所需的页数(n)

  • 将其在
    Intent
    中传递给新的
    活动

  • 实现自己的
    FragmentViewPager
    ,并将
    n
    值传递给其构造函数

  • 利润


  • 示例

    谢谢@agamov-您认为ViewPager能满足我的需要吗?你能给我举个例子吗