如何使用单选按钮选择任何日期

时间:2017-06-21 10:52:18

标签: android

我正在创建新的应用程序,这个应用程序怀孕,我想选择一个日期,我有最后的日期和截止日期,但我想选择使用单选按钮,然后选择。     请帮我     谢谢。

这是我的代码:

radioGroup_date = (RadioGroup) findViewById(R.id.radio_Date);
radioGroup_date.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {

                int selectedId = radioGroup_date.getCheckedRadioButtonId();//new
                radioButton_date = (RadioButton) findViewById(selectedId);//new
                String text = radioButton_date.getText().toString();//new
                Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();//new

            }
        });

        final Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);

        editText_calander.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //DateDialog();
                showDialog(DATE_PICKER_ID);
            }
        });

        button_Submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dob = editText_calander.getText().toString();

                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.putString("dob", dob);
                editor.commit();

                Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
                Bundle b = new Bundle();

//                int selectedId = radioGroup_date.getCheckedRadioButtonId();
//                radioButton_date = (RadioButton) findViewById(selectedId);
                b.putString("baby_name", editText_baby_name.getText().toString());
                b.putString("mother_name", editText_mother_name.getText().toString());
                intent.putExtras(b);

                startActivity(intent);
            }
        });

3 个答案:

答案 0 :(得分:0)

是否选中使用单选按钮

if(radioButton1.isChecked())
{
  //add last date  
}
else
{
  //add due date
}

答案 1 :(得分:0)

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical"
            android:weightSum="2">


            <RadioGroup
                android:id="@+id/radio_Date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginTop="50dp"
                android:padding="10dp">

                <RadioButton
                    android:id="@+id/last_date"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/last_date" />

                <RadioButton
                    android:id="@+id/due_date"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/due_date" />
            </RadioGroup>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:gravity="center"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/edit_calander"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ems="9"
                    android:hint="Calander"
                    android:textSize="20sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:gravity="center">

                <EditText
                    android:id="@+id/edit_baby_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:hint="Baby Name" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:gravity="center">

                <EditText
                    android:id="@+id/edit_mother_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:hint="Mother Name" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:gravity="center">

                <Button
                    android:id="@+id/button_submit"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:hint="Submit"
                    android:textSize="18sp" />
            </LinearLayout>

答案 2 :(得分:0)

public class MainActivity extends Activity {

    RadioGroup radioGroup_date;
    RadioButton radioButton_date;
    EditText editText_baby_name;
    TextView editText_calander;
    EditText editText_mother_name;
    Button button_Submit;
    // private String cabinChoice;

    private int day;
    private int month;
    private int year;

   String last_Date,due_date;

    public static final String MyPREFERENCES = "MyPrefs";
    SharedPreferences sharedpreferences;
    static final int DATE_PICKER_ID = 1;
    String dob;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        sharedpreferences = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE);

        radioGroup_date = (RadioGroup) findViewById(R.id.radio_Date);
        editText_baby_name = (EditText) findViewById(R.id.edit_baby_name);
        editText_mother_name = (EditText) findViewById(R.id.edit_mother_name);
        button_Submit = (Button) findViewById(R.id.button_submit);
        editText_calander = (TextView) findViewById(R.id.edit_calander);

//        int selectedId = radioGroup_date.getCheckedRadioButtonId();//new
//        radioButton_date = (RadioButton) findViewById(selectedId);//new
        radioGroup_date.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {

                int selectedId = radioGroup_date.getCheckedRadioButtonId();//new
                radioButton_date = (RadioButton) findViewById(selectedId);//new
                String text = radioButton_date.getText().toString();//new
                Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();//new

            }
        });

        final Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);

        editText_calander.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //DateDialog();
                showDialog(DATE_PICKER_ID);
            }
        });

        button_Submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dob = editText_calander.getText().toString();

                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.putString("dob", dob);
                editor.commit();

                Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
                Bundle b = new Bundle();

//                int selectedId = radioGroup_date.getCheckedRadioButtonId();
//                radioButton_date = (RadioButton) findViewById(selectedId);
                b.putString("baby_name", editText_baby_name.getText().toString());
                b.putString("mother_name", editText_mother_name.getText().toString());
                intent.putExtras(b);

                startActivity(intent);
            }
        });

    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_PICKER_ID:
                // open datepicker dialog.
                // set date picker for current date
                // add pickerListener listner to date picker
                return new DatePickerDialog(this, pickerListener, year, month, day);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        @Override
        public void onDateSet(DatePicker view, int selectedYear,
                              int selectedMonth, int selectedDay) {

            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;
            // Show selected date
            editText_calander.setText(new StringBuilder().append(year).append("-")
                    .append(month + 1).append("-").append(day));//old
        }
    };
}