0

我正在從事這項活動(類ChallengeNew52),其中我有一組RadioGroup。在這個RadioGroup中,我有一個EditText(id.etGoalNew),它位於RadioButton下(id.rButtonwithGoal)。如何在選中單選按鈕時在另一個活動中顯示EditText輸入?

當此RadioButton被選中(激活)時,假設用戶可以訪問/鍵入EditText並顯示他們鍵入的內容到另一個活動。我認爲我完成了啓用部分,但現在的問題是顯示它到另一個活動(類challengetab)。我似乎無法正確顯示此特定的RadioButton下的EditText字符串。在這個「開關」的另一種情況下顯示(endlessMode)。

代碼沒有給出錯誤,應用程序啓動正常,但它不顯示我想要的。希望你能檢查什麼是衝突或編碼。

下面是從活動代碼:

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.RadioGroup; 

public class ChallengeNew52 extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener { 

RadioGroup rgInputNew; 
EditText etGoalNew; 
EditText moIncome; 
EditText etGoalNumber; 
String challengetitle; 
String resultString; 
String goalTitle; 

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

    //Strings 
    final String saveTitle = getResources().getString(R.string.ctabtitle_fiftytwo); 

    //Radio Group Enable EditText (goal) input 
    rgInputNew = (RadioGroup) findViewById(R.id.rgGoal); 
    rgInputNew.setOnCheckedChangeListener(ChallengeNew52.this); 

    // EditTexts 
    moIncome = (EditText) findViewById(R.id.inputIncomeBar52); 
    etGoalNumber = (EditText) findViewById(R.id.inputGoalNumber52); 
    etGoalNew = (EditText) findViewById(R.id.inputGoalBar52); 
    actv(false); 

    // Button Call 
    Button startSaving = (Button) findViewById(R.id.buttonStartSaving); 

    /* 
    BUTTON CLICK! 
    */ 
    startSaving.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      //--Call Equation 
      mathFiftyTwo(); 

      //--To Display to Another Activity ("challengetab" java) 
      Intent iStartSaving = new Intent(getApplicationContext(), challengetab.class); 

      iStartSaving.putExtra("resultShow", resultString); 
      iStartSaving.putExtra("goalName", goalTitle); 
      iStartSaving.putExtra("titleChallenge", challengetitle); 
      iStartSaving.putExtra("resultShow", resultString); 

      ChallengeNew52.this.startActivity(iStartSaving); 
     } 
    });//--onClickListener 

    //--Call outs for the string Challenge Title 
    challengetitle = saveTitle; 
}//--onCreate 

//--To activate Edit Text with Radio Select. (METHOD) 
@Override 
public void onCheckedChanged (RadioGroup rgInputNew, int rButtonWithGoal) { 
    //--String 
    final String endlessMode = getResources().getString(R.string.ctabnogoal); 

    switch (rButtonWithGoal) { 
     case R.id.rButtonWithGoal: 
      actv(true); 
      dsply(true); 
      break; 

     case R.id.rbEndlessSaving: 
      actv(false); 
      //--Call outs for the string Endless Saving Mode 
      goalTitle = endlessMode; 

      break; 
    } 
}//--end onCheckedChanged 

//--display text method for goalTitle 
private void dsply (final boolean active) { 
    //--Strings 
    final String goalNew = etGoalNew.getText().toString(); 

    etGoalNew.setEnabled(active); 
    if (active) { 
     goalTitle = goalNew; 
    } 
} 

//--actv method for activating of Radio Button's EditText 
private void actv(final boolean active) { 

    etGoalNew.setEnabled(active); 
    if (active) { 
     etGoalNew.requestFocus(); 
    } 
    etGoalNumber.setEnabled(active); 
    if (active) { 
     etGoalNumber.requestFocus(); 
    } 
} 
}//--End class 

這裏是我如何編碼的其他活動:

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class challengetab extends AppCompatActivity { 

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

     //TextView for Goals 
     TextView goalView = (TextView)findViewById(R.id.cGoalName); 
     goalView.setText(getIntent().getExtras().getString("goalName")); 

     //TextView for Computation 

     TextView resultView = (TextView)findViewById(R.id.cValueSave); 
     resultView.setText(getIntent().getExtras().getString("resultShow")); 

     // Text View for Title 

     //TextView: Receive Challenge Title when button is clicked 
     TextView incomeView = (TextView)findViewById(R.id.cTitleChallenge); 
     incomeView.setText(getIntent().getExtras().getString("incomeTitle")); 
     incomeView.setText(getIntent().getExtras().getString("titleChallenge")); 
    } 
} 
+0

是否在'startSaving'被點擊後'challengetab'活動沒有填充'TextView'? –

+0

它正在與其他textView做它的工作,它是那個特定的EditText(id.etGoalNew),我試圖在挑戰標籤中顯示輸入在textview(id.goalView)不顯示。 – anpiel

回答

0

你永遠不會得到來自EditText命名etGoalNew最終值之後用戶已經完成填充它。

嘗試在點擊收聽這樣做,當你爲"goalName"額外設立Intent

if(rgInputNew.getCheckedRadioButtonId() == R.id.rButtonWithGoal) { 
    iStartSaving.putExtra("goalName", etGoalNew.getText().toString()); 
} else { 
    iStartSaving.putExtra("goalName", goalTitle); 
} 
+0

感謝George Mulligan爵士!這個伎倆。非常感謝你! :)用戶將輸入的etGoalName,顯示在challengeView中的goalView中!非常讚賞:)大拇指+1 – anpiel

0

你需要得到的第二個活動捆綁: 試試這個:

Bundle bundle = getIntent().getExtras(); 
if(bundle!=null){ 
    String value1 = bundle.getString("resultShow"); 
    String value2 = bundle.getString("goalName"); 
    String value3 = bundle.getString("titleChallenge"); 
    String value4 = bundle.getString("resultShow"); 
} 
+0

這不是一個簡單的方法來做同樣的事情嗎?不知道如何解決這個問題。 –

0
Intent iStartSaving = new Intent(getApplicationContext(), challengetab.class); 


      //******** Use getText() for getting the text from edit text & initialize the string i.e->"resultString","goalTitle" etc 
      resultString = moIncome.getText().toString(); 
      goalTitle = etGoalNumber.getText().toString(); 
      challengetitle = etGoalNew.getText().toString(); 

      iStartSaving.putExtra("resultShow", resultString); 
      iStartSaving.putExtra("goalName", goalTitle); 
      iStartSaving.putExtra("titleChallenge", challengetitle); 
      iStartSaving.putExtra("resultShow", resultString); 

      ChallengeNew52.this.startActivity(iStartSaving); 
相關問題