2017-04-08 14 views
0

我有一個按鈕,我希望當我點擊它去頁面片段單選按鈕被檢查,如果,例如,它是一個你好,單詞你好打印在textview中頁面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類指的回調或事件。在這種情況下,你可以創建一個類是這樣的:

class OptionItemEvent { 
    private String option; 

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

    public String getOption(){ 
    return option; 
    } 
} 

在你BlankFragment.java,你已經做出在適當的偵聽器方法的情況下調用,例如,在這種情況下:

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

在你MainActivity.java,添加以下代碼:

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

我希望這能解決你的問題。