2012-12-03 91 views
0

所以,我想在這裏做的是有地方,如果滿足要求主要活動(正確的用戶名,密碼和男性單選按鈕被選中,而不是女性),虛擬機開關到「成功」活動。如果它不符合上述三項要求中的任何一項,則在按下該按鈕時,VM切換到「失敗」活動。除了單選按鈕外,我的工作正常。單選按鈕(安卓)

我創建了一個RadioGroup中的佈局,但我不知道如何實現它的類本身。我認爲你必須找到ID,重寫監聽器等等,但它不能正常工作。有任何想法嗎?在發佈之前,我拿出了大部分的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

試試這個讓所選擇的單選按鈕的值,並用它來檢查在特定條件下的代碼:

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();  

希望這有助於!下面是一個示例代碼示例,您可以從GitHub下載以查看RadioButton的工作方式https://github.com/asabbarwal/SimpleRadioButton

+0

太棒了。得到它的工作感謝你們倆:D大樣本。非常清楚 – user1780149

+0

很高興我和我的示例GitHub示例可以幫助:) –

0

我第一次遇到的RadioButtons太不容易了。但基本上,使用它很簡單。

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一直將返回檢查單選按鈕的索引:

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

如果你想趕上單選按鈕點擊事件,在這裏很好的解決方案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(); 

       } 

      } 
     }); 
    } 


}