2017-04-08 7 views
0

私はボタンを持っています。クリックすると、フラグメントになります。ラジオボタンがチェックされています。たとえば、こんにちは、helloという文字がテキストビューにページMainActivityページフラグメントからラジオボタンを確認する

ページフラグメント

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="com.example.java.frag.BlankFragment"> 




     <RadioGroup 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 

      <RadioButton 
       android:text="nice to meet you" 
       android:textStyle="bold" 
       android:layout_gravity="center" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/radioButton" 
       android:layout_weight="1" /> 

      <RadioButton 
       android:text="welcome" 
       android:textStyle="bold" 
       android:layout_gravity="center" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/radioButton2" 
       android:layout_weight="1" /> 

      <RadioButton 
       android:text="hello" 
       android:textStyle="bold" 
       android:layout_gravity="center" 
       android:checked="true" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/radioButton3" 
       android:layout_weight="1" /> 
     </RadioGroup> 



</LinearLayout> 

ページMainActivity

package com.example.java.frag; 

import android.app.FragmentTransaction; 
import android.app.FragmentManager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

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

     TextView x = (TextView)findViewById(R.id.textView); 


    } 

    public void changetext(View view) { 
     FragmentManager frag = getFragmentManager(); 
     FragmentTransaction tran = frag.beginTransaction(); 
     BlankFragment s = new BlankFragment(); 
     tran.replace(R.id.con,s); 
     tran.commit(); 
    } 
} 

私はそれをどのように行うのですか?私が例にすべての

答えて

0

ファーストを得ることを期待

、あなたはあなたのクエリに関すると正確非常に明確にする必要があります。次に、EventBusをここで使用してください。異なるコンポーネント間のコールバックが必要なときに便利です。 EventBusを使用するには、アプリのbuild.gradleに、次の依存関係を追加する必要があります

compile 'org.greenrobot:eventbus:3.0.0' 

その後、あなたはコールバックやイベントを参照するための簡単なPOJOクラスを作成することができます。この場合、あなたはこのようなクラスを作成することができます:あなたのBlankFragment.javaで

class OptionItemEvent { 
    private String option; 

    public OptionItemEvent(String option){ 
    this.option = option; 
    } 

    public String getOption(){ 
    return option; 
    } 
} 

を、あなたは、この場合、例えば、適切なリスナーメソッドにイベントへの呼び出しを行うました:

私は、これはあなたの問題を解決を願ってい

@Override 
protected void onResume(){ 
    super.onResume(); 
    EventBus.getDefault().register(this); 
} 

@Override 
protected void onPause(){ 
    EventBus.getDefault().unregister(this); 
    super.onPause(); 
} 

@Subscribe (threadMode = ThreadMode.MAIN) 
public void onOptionItemSelected(OptionItemEvent event){ 
    //Set the value for your TextView 
    x.setText(event.getOption()); 
} 

:あなたのMainActivity.javaで

@Override 
public void onCheckedChanged(RadioGroup radioGroup, int radioButtonId) { 
    RadioButton radioButton = (RadioButton)view.findViewById(radioButtonId); 
    EventBus.getDefault().post(new OptionItemEvent(radioButton.getText().toString())); 
} 

、次のコードを追加します。

関連する問題