2016-02-28 170 views
0

我想改變點擊按鈕時的活動,並根據哪個單選按鈕打開新的佈局。我有三個單選按鈕。我想根據選中的單選按鈕更改佈局,然後單擊下一個按鈕。這裏是我的代碼如何根據單選按鈕更改按鈕單擊的佈局?



    package com.edkul.vimal.edkul; 

    import android.os.Bundle; 
    import android.support.design.widget.FloatingActionButton; 
    import android.support.design.widget.Snackbar; 
    import android.support.v7.app.AppCompatActivity; 
    import android.support.v7.widget.Toolbar; 
    import android.util.Log; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 
    import android.widget.RadioButton; 
    import android.widget.RadioGroup; 
    import android.widget.Toast; 
    import android.app.Activity; 
    import android.content.Intent; 

    public class Join_AS_A extends AppCompatActivity { 

     private RadioGroup radioGroup; 
     private RadioButton radioButton; 
     private Button btn; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_join__as_); 
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
      setSupportActionBar(toolbar); 

      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
      fab.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
          .setAction("Action", null).show(); 
       } 
      }); 

      radioGroup = (RadioGroup) findViewById(R.id.joinGroup); 
      radioGroup.clearCheck(); 
      int selectedId = radioGroup.getCheckedRadioButtonId(); 
      radioButton = (RadioButton) findViewById(selectedId); 
      btn = (Button)findViewById(R.id.nextButton); 
      btn.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        String selection = (String) radioButton.getText(); 
        Log.i("Print here", selection); 
        if (selection.equals("Student") && view ==btn){ 
         Intent intentMain = new Intent(Join_AS_A.this, Student_Signup.class); 
         Join_AS_A.this.startActivity(intentMain); 
         Log.i("Content ", " Main layout "); 
        } 

       } 
      }); 
     } 

    } 


下面是XML代碼

Here is the xml file :

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.edkul.vimal.edkul.Join_AS_A" tools:showIn="@layout/activity_join__as_"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="@string/joinAsA" android:textSize="30sp" android:id="@+id/textView2" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <RadioGroup android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/textView2" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="47dp" android:layout_alignRight="@+id/textView2" android:layout_alignEnd="@+id/textView2" android:id="@+id/joinGroup"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/joinasaStudent" android:textSize="20sp" android:textColor="#33b5e5" android:id="@+id/radioButton" android:checked="false" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/joinasaTeacher" android:textSize="20sp" android:textColor="#33b5e5" android:id="@+id/radioButton2" android:checked="false" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/joinasaparent" android:textSize="20sp" android:textColor="#33b5e5" android:id="@+id/radioButton3" android:checked="false" /> </RadioGroup> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/joinNext" android:id="@+id/nextButton" android:background="@drawable/custom_button" android:layout_alignBottom="@+id/joinGroup" android:layout_centerHorizontal="true" android:layout_marginBottom="79dp" /> </RelativeLayout>

回答

0

你必須改變對點擊是一樣的東西代碼: -

radioGroup = (RadioGroup) findViewById(R.id.joinGroup); 
    btn = (Button) findViewById(R.id.nextButton); 
    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      int selectedId = radioGroup.getCheckedRadioButtonId(); 
      if (selectedId == R.id.radioButton) { 
       Intent intentMain = new Intent(MainActivity.this, 
         Student_Signup.class); 
       startActivity(intentMain); 
       Log.i("Content ", " Main layout "); 
      } 

     } 
    }); 

當您在oncreate()中獲得選中的單選按鈕ID時,即使用戶選擇了其他單選按鈕,其引用也不會發生變化。所以你必須找到所選的單選按鈕ID,當用戶點擊下一個按鈕,然後必須相應地執行操作。

+0

感謝您的回覆。我已將相同的代碼複製到mine.But,但仍無法更改layout.sorry。 –

+0

你得到了什麼錯誤? –

+0

我沒有看到任何錯誤..甚至我的日誌語句不打印...你可以請建議如何檢查錯誤..我非常新的android這是我的第一個應用程序 –