触发点击事件

dear_DengMingJing 2016-05-14 03:29:08

**这里是activity_main.xml
**

<RelativeLayout xmlns:android="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" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="@string/text_tabll" />

<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioButton3"
android:layout_below="@+id/radioButton3"
android:text="@string/kl" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/radioButton4"
android:layout_marginLeft="56dp"
android:layout_marginTop="30dp"
android:text="@string/tj" />

<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioButton2"
android:layout_below="@+id/radioButton2"
android:text="@string/zms" />

<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/textView1"
android:layout_marginRight="15dp"
android:layout_marginTop="16dp"
android:text="@string/md" />

<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioButton1"
android:layout_below="@+id/radioButton1"
android:text="@string/kb" />

</RelativeLayout>////


///*这里是Mainactivity.java
*
package com.marry.ch_2;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends Activity {

private RadioButton rbtn1;
private RadioButton rbtn2;
private RadioButton rbtn3;
private RadioButton rbtn4;
private TextView tv1;
private Button btn1;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rbtn1=(RadioButton) this.findViewById(R.id.radioButton1);
rbtn2=(RadioButton) this.findViewById(R.id.radioButton2);
rbtn3=(RadioButton) this.findViewById(R.id.radioButton3);
rbtn4=(RadioButton) this.findViewById(R.id.radioButton4);
btn1=(Button) this.findViewById(R.id.button1);
tv1=(TextView) this.findViewById(R.id.textView1);


........////后面不知道怎么写了,初学安卓希望大家帮助我
点击提交出现“我喜欢的明星是明道,詹姆斯,科比”,后面不知道怎么写了
}

}







/////////////这里是strings.xml应该没错
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">ch_2</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="text_tabll">请选择你最喜欢的明星,没有之一</string>
<string name="md">明道</string>
<string name="kb">科比</string>
<string name="zms">詹姆斯</string>
<string name="kl">库里</string>
<string name="tj">提交</string>

</resources>
...全文
163 2 打赏 收藏 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dear_DengMingJing 2016-05-25
  • 打赏
  • 举报
回复
谢谢 嘻嘻 我知道了
小傻家大叔 2016-05-14
  • 打赏
  • 举报
回复
你这是单选按钮,不可能同时提交这么多选项的内容,只能提交一个选项数据. 单选按钮RadioButton应该放在RadioGroup里面,对RadioGroup进行监听才行.
布局:
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="38dp"
android:layout_marginTop="52dp" >

<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="学习" />

<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游戏" />

<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="吃饭" />
</RadioGroup>

MainActivity.java里面
public class MainActivity extends Activity {

private RadioGroup rg;
private MyOnCheckedChangeListener listener;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

rg = (RadioGroup) findViewById(R.id.radioGroup1);
listener = new MyOnCheckedChangeListener();
rg.setOnCheckedChangeListener(listener);

}

private class MyOnCheckedChangeListener implements OnCheckedChangeListener {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

RadioButton rb = (RadioButton)findViewById(checkedId);
String text = rb.getText().toString();
switch (checkedId) {
case R.id.radio0:
Log.i("XiaoXiaoTu", text);
break;
case R.id.radio1:
Log.i("XiaoXiaoTu", text);
break;
case R.id.radio2:
Log.i("XiaoXiaoTu", text);
break;
}

}
}

}
点击每个单选按钮都会打印对应按钮的内容,你只是显示的话,就简单用Toast一下就行.

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧