android 圆形单选框,《android 多选框和单选框》

这是布局xml文件:java

http://schemas.android.com/apk/res/android

"

xmlns:tools="

http://schemas.android.com/tools

"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity" >

android:id="@+id/hello"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

android:id="@+id/input"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="@android:drawable/editbox_background"

android:layout_below="@id/hello"

/>

android:id="@+id/text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/text"

android:layout_below="@id/input"

/>

android:id="@+id/radio"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/text"

>

android:id="@+id/man"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/man"/>

android:id="@+id/gril"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/gril"

/>

android:id="@+id/choose"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/choose"

android:layout_below="@id/radio"

/>

android:id="@+id/one"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/one"

android:layout_below="@id/choose"

/>

android:id="@+id/two"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/two"

android:layout_below="@id/choose"

android:layout_toRightOf="@id/one"

/>

android:id="@+id/three"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/three"

android:layout_toRightOf="@id/two"

android:layout_below="@id/choose"

/>

android:id="@+id/ok"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/ok"

android:layout_below="@id/two"

/>

这是String配置文件android

layout2

Settings

请输入您的名字:

请选择您的性别:

请选择你喜欢的水果:

芒果

香蕉

椰子

提交

最后是实现的JAVA:app

package com.example.layout2;

import android.os.Bundle;

import android.app.Activity;

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.EditText;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.RadioGroup.OnCheckedChangeListener;

import android.widget.Toast;

public class MainActivity extends Activity {

private EditText edit;

private Button button;

private RadioGroup group;

private CheckBox box1;

private CheckBox box2;

private CheckBox box3;

private BoxChange change;

private String str="";

private String sex="";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

edit=(EditText) findViewById(R.id.input);

button=(Button) findViewById(R.id.ok);

group=(RadioGroup) findViewById(R.id.radio);

box1=(CheckBox) findViewById(R.id.one);

box2=(CheckBox) findViewById(R.id.two);

box3=(CheckBox) findViewById(R.id.three);

group.setOnCheckedChangeListener(new RadioChange());

change=new BoxChange();

box1.setOnCheckedChangeListener(change);

box2.setOnCheckedChangeListener(change);

box3.setOnCheckedChangeListener(change);

button.setOnClickListener(new ButtonClick());

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

class RadioChange implements OnCheckedChangeListener{

public void onCheckedChanged(RadioGroup group, int checkedId) {

RadioButton radio=(RadioButton) group.findViewById(checkedId);

sex=radio.getText().toString();

}

}

class BoxChange implements android.widget.CompoundButton.OnCheckedChangeListener{

public void onCheckedChanged(CompoundButton buttonView,

boolean isChecked) {

CheckBox box=(CheckBox) buttonView;

String boxStr=box.getText().toString()+"、";

if(isChecked){

str +=boxStr;

}else{

System.out.println(boxStr);

str=str.replace(boxStr, "");

}

}

}

class ButtonClick implements OnClickListener{

public void onClick(View v) {

String name=edit.getText().toString();

Toast.makeText(MainActivity.this, "亲爱的:"+name+",您的性别是:"+sex+",您喜欢的水果有:"+str, Toast.LENGTH_SHORT).show();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值