代码之家  ›  专栏  ›  技术社区  ›  anpiel

选中单选按钮时,如何在另一个活动中显示EditText输入?

  •  0
  • anpiel  · 技术社区  · 10 年前

    我目前正在进行这项活动(ChallengeNew52类),其中我有一组RadioGroup。在这个RadioGroup中,我有一个EditText(id.etGoalNew),它位于RadioButton(id.rButtonwithGoal)下。

    选中此RadioButton(活动)时,假定用户可以访问/键入EditText,并显示他们键入的其他活动。我认为我完成了启用部分,但现在的问题是将其显示给另一个活动(classchallengetab)。我似乎无法在这个特定的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"));
        }
    }
    
    3 回复  |  直到 10 年前
        1
  •  0
  •   George Mulligan    10 年前

    您永远无法从 EditText 命名 etGoalNew 在用户完成填充之后。

    当您设置 Intent 对于 "goalName" 额外:

    if(rgInputNew.getCheckedRadioButtonId() == R.id.rButtonWithGoal) {
        iStartSaving.putExtra("goalName", etGoalNew.getText().toString());
    } else {
        iStartSaving.putExtra("goalName", goalTitle);
    }
    
        2
  •  0
  •   ΦXocę 웃 Пepeúpa ツ    10 年前

    您需要在第二个活动中获得捆绑包: 试试这个:

    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");
    }
    
        3
  •  0
  •   Ajaya Tiwari    10 年前
     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);