我在Android中创建了一个简单的应用程序,有两个活动,在第一个活动中,它是一个包含一些editexts和无线电组以及一个复选框的表单。但我有单选按钮的问题。它的值不会转到另一个活动.i尝试过如下:
act1.java
package com.example.myweb;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
public String EXTRA_MESSAGE = "com.example.myweb";
Button b;
EditText ed1,ed2,ed3,ed4;
RadioGroup rg;
RadioButton rb;
CheckBox c1;
// boolean yes = c1.isChecked();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg=(RadioGroup)findViewById(R.id.radioGroup1);
b =(Button)findViewById(R.id.send);
ed1=(EditText)findViewById(R.id.edit_msg);
ed2=(EditText)findViewById(R.id.edit_msg1);
ed3=(EditText)findViewById(R.id.edit_msg3);
ed4=(EditText)findViewById(R.id.edit_msg4);
// r1=(RadioButton)findViewById(R.id.radioButton1);
// r2=(RadioButton)findViewById(R.id.radioButton2);
c1=(CheckBox)findViewById(R.id.ck1);
int id=rg.getCheckedRadioButtonId();
rb=(RadioButton)findViewById(id);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Intent i = new Intent(MainActivity.this,Act2.class);
Bundle b=new Bundle();
final String fname=ed1.getText().toString();
final String lname=ed2.getText().toString();
final String fon=ed3.getText().toString();
final String city= ed4.getText().toString();
final String gen = rb.getText().toString();
if(c1.isChecked()){
Intent i = new Intent(MainActivity.this,Act2.class);
i.putExtra("Extra__fname", fname);
i.putExtra("Extra__lname", lname);
i.putExtra("Extra__fon", fon);
i.putExtra("Extra__city", city);
i.putExtra("gen", gen);
startActivity(i);
}
else{
Toast.makeText(getApplicationContext(), "Please accept it", 1).show();
}
}
});
}
}
act2.java
package com.example.myweb;
import org.w3c.dom.Text;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Act2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
TextView t = (TextView)findViewById(R.id.tv1);
TextView t1 = (TextView)findViewById(R.id.tv2);
TextView t2 = (TextView)findViewById(R.id.tv3);
TextView t3 = (TextView)findViewById(R.id.tv4);
TextView t4 = (TextView)findViewById(R.id.tv5);
Bundle bundle = getIntent().getExtras();
String fname= bundle.getString("Extra__fname");
String lname= bundle.getString("Extra__lname");
String fon= bundle.getString("Extra__fon");
String city= bundle.getString("Extra__city");
String gen= bundle.getString("gen");
t.setText(fname);
t1.setText(lname);
t2.setText(fon);
t3.setText(city);
t4.setText(gen);
}
}
logcat的
05-02 09:55:28.492: E/ActivityThread(554): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cf6fe0 that was originally bound here
05-02 09:55:28.492: E/ActivityThread(554): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cf6fe0 that was originally bound here
05-02 09:55:28.492: E/ActivityThread(554): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
05-02 09:55:28.492: E/ActivityThread(554): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
05-02 09:55:28.492: E/ActivityThread(554): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
05-02 09:55:28.492: E/ActivityThread(554): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
05-02 09:55:28.492: E/ActivityThread(554): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
05-02 09:55:28.492: E/ActivityThread(554): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
05-02 09:55:28.492: E/ActivityThread(554): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
05-02 09:55:28.492: E/ActivityThread(554): at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
05-02 09:55:28.492: E/ActivityThread(554): at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
05-02 09:55:28.492: E/ActivityThread(554): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
05-02 09:55:28.492: E/ActivityThread(554): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
05-02 09:55:28.492: E/ActivityThread(554): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
05-02 09:55:28.492: E/ActivityThread(554): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-02 09:55:28.492: E/ActivityThread(554): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-02 09:55:28.492: E/ActivityThread(554): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-02 09:55:28.492: E/ActivityThread(554): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-02 09:55:28.492: E/ActivityThread(554): at java.lang.Thread.run(Thread.java:856)
05-02 09:55:28.592: E/StrictMode(554): null
05-02 09:55:28.592: E/StrictMode(554): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cf6fe0 that was originally bound here
05-02 09:55:28.592: E/StrictMode(554): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
05-02 09:55:28.592: E/StrictMode(554): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
05-02 09:55:28.592: E/StrictMode(554): at android.app.ContextImpl.bindService(ContextImpl.java:1418)
05-02 09:55:28.592: E/StrictMode(554): at android.app.ContextImpl.bindService(ContextImpl.java:1407)
05-02 09:55:28.592: E/StrictMode(554): at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
05-02 09:55:28.592: E/StrictMode(554): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
05-02 09:55:28.592: E/StrictMode(554): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
05-02 09:55:28.592: E/StrictMode(554): at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
05-02 09:55:28.592: E/StrictMode(554): at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
05-02 09:55:28.592: E/StrictMode(554): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
05-02 09:55:28.592: E/StrictMode(554): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
05-02 09:55:28.592: E/StrictMode(554): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
05-02 09:55:28.592: E/StrictMode(554): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-02 09:55:28.592: E/StrictMode(554): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-02 09:55:28.592: E/StrictMode(554): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-02 09:55:28.592: E/StrictMode(554): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-02 09:55:28.592: E/StrictMode(554): at java.lang.Thread.run(Thread.java:856)
05-02 09:55:28.592: W/ActivityManager(274): Unbind failed: could not find connection for android.os.BinderProxy@410f7970
答案 0 :(得分:1)
使用
String gen= bundle.getString("gen");
而不是
String gen= bundle.getString("Extra_gen");
用于从上一个活动中发出的Intent中检索选定的RadioButton
值。因为您使用gen
键发送RadioButton值,但尝试使用Extra_gen
答案 1 :(得分:1)
密钥应该是相同的
i.putExtra("gen", gen);
及其
String gen= bundle.getString("Extra_gen");
应该是
String gen= bundle.getString("gen");
编辑:
您正在使用getApplicationContext()。而是使用活动上下文
Toast.makeText(getApplicationContext(), "Please accept it", 1).show();
要知道何时使用应用程序上下文以及何时使用活动上下文,请通过以下链接检查commonsware的答案
When to call activity context OR application context?
Toast.makeText(act1.this, "Please accept it", 1).show();
答案 2 :(得分:0)
使用
String gen= bundle.getString("gen");
你需要这样的东西
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
void onCheckedChanged(RadioGroup rg, int checkedId) {
for(int i=0; i<rg.getChildCount(); i++) {
RadioButton btn = (RadioButton) rg.getChildAt(i);
if(btn.getId() == checkedId) {
String text = btn.getText();
// do something with text
return;
}
}
}
});
(OR)
当您需要获取所选的电台选项时。
int id = rg.getCheckedRadioButtonId();
if (id == -1){
//no item selected
}
else{
if (id == R.id.radio_button1){
//Do something with the button
}
}
答案 3 :(得分:0)
将setOnCheckedChangeListener
用于radioGroup。
RadioGroup
的示例代码,其中包含两个RadioButtons
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup arg0, int arg1) {
radioButton = (RadioButton) findViewById(radioGroup
.getCheckedRadioButtonId());
if (radioButton.getText().equals("Name")) {
// code for first radioButton
} else {
// code for second radioButton
}
}
});
我希望这会对你有所帮助。
答案 4 :(得分:0)
int radioButtonID = rg.getCheckedRadioButtonId();
View radioButton = rg.findViewById(radioButtonID);
int index = rg.indexOfChild(radioButton);
String gen="male";
if(index==0)
gen="male";
else
gen="female";
....
i.putExtra("gen", gen);
startActivity(i);
确保activity_main.xml
男性的指数为0。