这是我的代码片段。这Textview
充当按钮并具有Onclicklistner
在他们。当cpu1000时Textview
单击它会导致cpu_g1000
其代码如下所示的类。
public class Game_1000 extends AppCompatActivity implements View.OnClickListener{
private TextView cpu1000, mobo1000;
TextView cpu, mobo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_1000);
cpu1000 = (TextView) findViewById(R.id.proName_G1);
mobo1000 = (TextView) findViewById(R.id.moboName_G1);
cpu1000.setOnClickListener(this);
mobo1000.setOnClickListener(this);
cpu = (TextView) findViewById(R.id.proNameG1000);
cpu.setText(getIntent().getStringExtra("Processor"));
mobo = (TextView) findViewById(R.id.moboNameG1000);
mobo.setText(getIntent().getStringExtra("Motherboard"));
}
@Override
public void onClick(View v) {
if (v == cpu1000) {
opencpu_g1000();
}
else if (v == mobo1000) {
openmobo_g1000();
}
}
public void opencpu_g1000() {
Intent intent = new Intent(this, cpu_g1000.class);
startActivity(intent);
}
public void openmobo_g1000() {
Intent intent = new Intent(this, mobo_g1000.class);
startActivity(intent);
}
在这个类中,有单选按钮。用户选择其中一项,该选项将更改为字符串。字符串被发送回Game_1000
班级。然后该字符串被替换为“选择处理器”来展示新的选择。我遇到的问题是,当我选择主板时,处理器选择会恢复为“选择处理器”和主板选择显示。我需要两者同时显示。
public class cpu_g1000 extends AppCompatActivity {
Button button_save;
RadioGroup rG;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cpu_g1000);
button_save = (Button) findViewById(R.id.Save_G1_cpu);
rG = (RadioGroup) findViewById(R.id.cpu_RadioGrp);
tv = (TextView) findViewById(R.id.proNameG1000);
button_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int selected_cpu = rG.getCheckedRadioButtonId();
RadioButton selectedRadioButton = (RadioButton) findViewById(selected_cpu);
String radioValue = selectedRadioButton.getText().toString();
Intent intent = new Intent(cpu_g1000.this, Game_1000.class);
intent.putExtra("Processor", radioValue);
startActivity(intent);
}
});
}
选择处理器:
主板选择:
选择示例:
先前的选择不会被记录,当做出新的选择时,先前的选择将恢复为“请选择处理器”,但我需要显示这两个信息。在我的原始代码中,我有不止两个选择,但我缩短了它以使其更易于阅读。
通过调用开始第二个(选择)活动以获取结果startActivityForResult(...)
然后当用户完成交互时,将您选择的数据设置在Intent
并将该意图传递给具有所需结果的方法setResult(...)
然后调用第二个活动的完成。
完成第二个活动后,您将收到意图数据onActivityResult(...)
第一个活动的方法,从意图中提取数据,然后将其显示给用户。
更多说明 https://developer.android.com/training/basics/intents/result.html
Example http://www.vogella.com/tutorials/AndroidIntent/article.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)