检索已检查的单选按钮数据,并在按下按钮时发送到另一个活动

时间:2011-04-29 09:29:37

标签: android button radio-button

我有一个问题:我有两个单选按钮作为图像按钮和一个按钮。

当我点击Imagebutton时,我想读取该值并将其发送到其他活动,当我按下按钮时该怎么办?

请尽可能提供一段代码

1 个答案:

答案 0 :(得分:1)

package com.httpconnection;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.util.ByteArrayBuffer;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class httpconnectionActivity extends Activity implements OnCheckedChangeListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout layout = new LinearLayout(this);
        layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        RadioButton rd1 = new RadioButton(this);
        rd1.setId(1);
        rd1.setOnCheckedChangeListener(this);  
        layout.addView(rd1);
        RadioButton rd2 = new RadioButton(this);
        rd2.setId(2);
        rd2.setOnCheckedChangeListener(this); 
        layout.addView(rd2);
        setContentView(layout);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        Intent intent = new Intent(this, xyz.class);
        intent.putExtra("MyKey", buttonView.getId());
        startActivity(intent);


    }
}