2016-02-28 3 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(), его ссылка не будет изменяться, даже если пользователь выберет какую-либо другую кнопку. Поэтому вам нужно найти выбранный идентификатор переключателя, когда пользователь нажмет на следующую кнопку, а затем должен выполнить операцию соответствующим образом.

+0

Спасибо за ответ. Я скопировал один и тот же код над моим. Но все еще не могу изменить layout.sorry. –

+0

какая ошибка вы получаете? –

+0

Я не вижу никакой ошибки .. и даже мои записи в журнале не печатаются ... можете ли вы предложить, как проверить ошибку .. Извините, я очень новичок в андроиде, это мое первое приложение –

Смежные вопросы