2012-12-03 3 views
0

그래서, 내가 뭘 하려는지, 요구 사항이 충족되면 (정확한 사용자 이름, 암호 및 남성 라디오 버튼이 선택되어 있고 여성이 아닌) 주요 활동이있는 것입니다. VM은 "성공"활동. 위의 3 가지 요구 사항 중 하나라도 충족되지 않으면 VM은 버튼을 누를 때 "실패"활동으로 전환합니다. 라디오 버튼을 제외하고는 제대로 작동합니다.라디오 버튼 (안드로이드)

레이아웃에 RadioGroup을 만들었지 만 클래스 자체에서 구현하는 방법을 모르겠습니다. 나는 당신이 신분증을 찾아야하고, 청취자 등등을 무시해야한다고 생각했다. 그러나 그것은 올바르게 작동하지 않고있다. 어떤 아이디어? 이 게시물을 게시하기 전에 대부분의 RadioGroup 속성을 제거하여 덜 혼란 스러울 것입니다.

주요 활동

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.RadioGroup; 

public class MainActivity extends Activity implements RadioGroup.OnCheckedChangeListener 
{ 
    Button button; 
    EditText login; 
    EditText password; 
    RadioGroup mRadioGroup; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     button = (Button)findViewById(R.id.button1); 
     login =(EditText)findViewById(R.id.editText1); 
     password =(EditText)findViewById(R.id.editText2); 
     button.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View view) 
     { 
      String L,P;    

      L = login.getText().toString(); 
      P = password.getText().toString(); 


      if(L.equals("name") && P.equals("123456")) 
      { 
       Intent intent = new Intent(); 
       intent.setClass(MainActivity.this,Welcome.class); 
       startActivity(intent); 
      } 
      else 
      { 
       Intent intent1 = new Intent(); 
       intent1.setClass(MainActivity.this,Failed.class); 
       startActivity(intent1); 
      } 
     } 
    }); 




} 



    /* public void onRadioButtonClicked(View view) 
    { 
     boolean checked = ((RadioButton) view).isChecked(); 

     switch(view.getId()) 
     { 
      case R.id.radio1: 
       if (checked) 
       { 
       Intent intent4 = new Intent(); 
       intent4.setClass(MainActivity.this,Welcome.class); 
       startActivity(intent4); 
       } 
       break; 
      case R.id.radio0: 
       if (checked) 
       { 
       Intent intent2 = new Intent(); 
       intent2.setClass(MainActivity.this,Failed.class); 
       startActivity(intent2); 
       } 
       break; 
       } 
       } 
     */ 



    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 



    @Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) 
    { 
     // TODO Auto-generated method stub 

    } 


} 

실패 활동

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class Failed extends Activity 
{ 
    Button button; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.failed); 
     button = (Button)findViewById(R.id.button1);  
     button.setOnClickListener(new View.OnClickListener() 


    { 
     @Override 
     public void onClick(View view) 
     { 
      Intent intent3 = new Intent(); 
      intent3.setClass(Failed.this,MainActivity.class); 
      startActivity(intent3); 
     } 

    }); 
} 
} 

성공 활동

import android.app.Activity; 
import android.os.Bundle; 

public class Welcome extends Activity 
{ 

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

} 

답변

0

이 선택의 RadioButton의 값을 얻기 위해 시도하고 특정 조건을 확인하기 위해 그것을 사용하여 코드 :

private RadioGroup radioOptionGrp; 
private RadioButton radioOptBtn; 

//Get Reference to Radio group which holds the radio buttons 
radioOptionGrp = (RadioGroup) findViewById(R.id.radioOpt); 

//Get id of selected radioButton 
int selectOptId = radioOptionGrp.getCheckedRadioButtonId(); 

//Get Reference to the Selected RadioButton 
radioOptBtn = (RadioButton) findViewById(selectOptId); 

//Get value of the selected Radio button 
String value = radioOptBtn.getText().toString();  

희망이 도움이됩니다! 다음은 RadioButton이 작동하는 방법을 GitHub에서 다운로드 할 수있는 샘플 코드입니다. https://github.com/asabbarwal/SimpleRadioButton

+0

굉장합니다. 두 분 모두에게 감사드립니다. D 그레이트 샘플. 매우 명확하게 만든다. – user1780149

+0

다행이다. GitHub 예제가 도움이 될 수있다. :) –

0

RadioButton의 첫 만남은 너무 쉽지 않았습니다. 그러나 기본적으로, 그것을 사용하는 것은 간단합니다.

radioButtonID = mRadioGroup.getCheckedRadioButtonId(); 
radioButton = mRadioGroup.findViewById(radioButtonID); 
idx = mRadioGroup.indexOfChild(radioButton); 

당신이 라디오 버튼 클릭 이벤트를 잡으려면, 여기에 좋은 해결책 :

private RadioGroup mRadioGroup; 
private View radioButton; 
int radioButtonID; 
int idx;   //index of radio item in the list 

public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
     mRadioGroup= (RadioGroup) findViewById(R.id.mRadioGroup);   
.... 
} 

를 착용 클릭 이벤트 메서드에 코드의이 부분, IDX는 항상 확인 라디오 버튼의 인덱스를 반환합니다 How to set On click listener on the Radio Button in android

0
at first you can add two radio button in your activity like other form widgets 
than try this code. 
pay attention that when you click and set a radio button,you must disable other radio buttons. 

arg1은 버튼이 선택되었음을 의미합니다.

package com.example.azarbaycannetworkcompany; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.widget.CompoundButton; 
import android.widget.Toast; 
import android.widget.CompoundButton.OnCheckedChangeListener; 
import android.widget.RadioButton; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     final RadioButton first = (RadioButton) findViewById(R.id.radioButton1); 
     final RadioButton seccond = (RadioButton) findViewById(R.id.radioButton2); 
     first.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton arg0, boolean arg1) { 
       // TODO Auto-generated method stub 
       if(arg1) 
       { 
        seccond.setChecked(false); 
        Toast.makeText(getBaseContext(),"1 set shod",Toast.LENGTH_LONG).show(); 
       } 

      } 
     }); 
     seccond.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton arg0, boolean arg1) { 
       // TODO Auto-generated method stub 
       if(arg1) 
       { 
        first.setChecked(false); 
       Toast.makeText(getBaseContext(),"2 set shod",Toast.LENGTH_LONG).show(); 

       } 

      } 
     }); 
    } 


}