设置从不同的单选按钮选择中选中的单选按钮值

时间:2018-03-23 11:48:48

标签: android

我想从第1个活动到第2个活动获取单选按钮检查值。第二个活动包含列表视图的布局。列表视图的xml文件有一个广播组。我想将第1个活动的单选按钮值设置为该文件。

[1st activity image]

[1st activity radio button

[2nd activity radio list view

AttendanceActivity.java

    ListView attendanceListView;
    String[] penNumber;
    Button saveAttendance;
    RadioButton presentRadio,absentRadio;
    RadioGroup attendanceRadio;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_attendance);

    View inflatedView = 
    getLayoutInflater().inflate(R.layout.attendance_list_item, null);
    presentRadio = inflatedView.findViewById(R.id.present_radio);
    absentRadio = inflatedView.findViewById(R.id.absent_radio);
    attendanceRadio = inflatedView.findViewById(R.id.AttendanceRadio);

    Intent intent = getIntent();
    int aorp = intent.getIntExtra("aorb", 1);
    if(aorp ==1)
    {
        attendanceRadio.check(R.id.present_radio);
    }
    else if(aorp ==2)
    {
        attendanceRadio.check(R.id.absent_radio);
    }
    penNumber = getResources().getStringArray(R.array.penNo);

    attendanceListView = findViewById(R.id.attendanceListView);
    saveAttendance = findViewById(R.id.saveAttendance);

    AttendanceListViewAdapter customAdapter = new 
    AttendanceListViewAdapter(getApplicationContext(), penNumber);
    attendanceListView.setAdapter(customAdapter);

    saveAttendance.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(AttendanceActivity.this, 
    AttendanceSummaryActivity.class);
            startActivity(i);
        }
    });
   }

faculty_home_page.kt

     alertDialog.setSingleChoiceItems(alertRadioStatus, -1, 
     DialogInterface.OnClickListener { dialogInterface, i ->

            Toast.makeText(applicationContext,
                    "Mostly Students are " + alertRadioStatus[i], Toast.LENGTH_LONG).show()

            when (i) {
                0 ->
                    aorp=1
                1 ->
                    aorp=2
            }
            val attendancePage = Intent(this@Faculty_Home_Page, AttendanceActivity::class.java)
            startActivity(attendancePage)
            attendancePage.putExtra("a_p_radio", aorp)
            dialogInterface.dismiss()

        })
        alertDialog.show()
        }

AttendanceListItem.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="70dp"
android:layout_margin="10dp"
android:padding="10dp"
xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.percent.PercentRelativeLayout
        android:layout_height="match_parent"
        android:layout_width="wrap_content">

<TextView
    android:id="@+id/pen_number_list"
    app:layout_widthPercent="44%"
    android:layout_height="match_parent"
    android:gravity="center"
    android:textSize="17sp"
    android:text="@string/Pen_View"
    android:textColor="#000" />

<RadioGroup
    android:id="@+id/AttendanceRadio"
    app:layout_widthPercent="50%"
    android:layout_height="match_parent"
    android:gravity="center"
    app:layout_marginEndPercent="1%"
    android:layout_alignParentEnd="true"
    android:orientation="horizontal"
    android:weightSum="4">

    <RadioButton
        android:id="@+id/present_radio"
        android:layout_width="0dp"
        android:buttonTint="@color/forest_green"
        android:layout_height="match_parent"
        android:layout_weight="1"
        app:layout_marginStartPercent="1%"
        app:layout_marginEndPercent="1%" />

    <RadioButton
        android:id="@+id/absent_radio"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:buttonTint="@color/red"
        android:layout_marginStart="15dp"
        android:layout_weight="1"/>

    <RadioButton
        android:id="@+id/leave_radio"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:buttonTint="@color/yellow_full"
        android:layout_marginStart="15dp"
        android:layout_weight="1"/>

    <RadioButton
        android:id="@+id/late_radio"
        android:layout_width="0dp"
        android:buttonTint="@color/orange_full"
        android:layout_height="match_parent"
        android:layout_marginStart="15dp"
        android:layout_weight="1" />
</RadioGroup>

</android.support.percent.PercentRelativeLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您可以使用SharedPreferences。当第一个单选按钮的值更改时,您可以将值保存在SharedPreferences中。然后,当您打开第二个活动(或对话框)时,您可以检查上一个活动中保存的值,并根据该值设置单选按钮。