2017-06-21 34 views
0

我正在創建新的應用程序,這個應用程序是懷孕,我想選擇一個日期,我有最後一個日期和截止日期,但我想選擇使用單選按鈕並選擇。 請幫幫我 謝謝。如何使用單選按鈕選擇任何人的日期

這裏是我的代碼:

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); 
      } 
     }); 
+0

你可否透露具體 – Anil

+0

是的,我要選擇從任何一個日期單選按鈕1.最後日期2.截止日期,如果用戶選擇最後日期而不是添加40周並顯示截止日期另一個活動,並且如果用戶想選擇直接截止日期比顯示另一個活動 –

+0

'但我想選擇使用單選按鈕,並選擇'它是完全不清楚你想要什麼。 –

回答

0

使用單選按鈕被選中或不

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

不,它不適用於我 –

+0

你可以發佈你的xml文件 – Anil

+0

你需要檢查哪個單選按鈕使用isChecked()函數根據你需要檢查條件 – Anil

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> 
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 
     } 
    }; 
} 
相關問題