toast语句Android,虚拟机中点击提交按钮,但是没有Toast出来设定的语句

本文展示了如何在Android应用中创建一个活动(Activity),让用户选择多个爱好(如游泳、唱歌、阅读)并通过点击提交按钮展示所选爱好。应用使用CheckBox组件和Toast消息来交互,同时定义了一个独立的监听器处理多个CheckBox的选择事件。
摘要由CSDN通过智能技术生成

1.MyHobby.java

package com.example.hobby;

//import java.security.PublicKey;

//import android.R.integer;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.Toast;

import android.widget.CompoundButton.OnCheckedChangeListener;

public class MyHobby extends Activity{

private CheckBox hobby1,hobby2,hobby3;

private Button submit;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.checkhobby);

//初始化

submit=(Button) findViewById(R.id.submit);

hobby1=(CheckBox) findViewById(R.id.checkBox1);

hobby2=(CheckBox) findViewById(R.id.checkBox2);

hobby3=(CheckBox) findViewById(R.id.checkBox3);

submit.setOnClickListener(new OnClickListener() {

public void onClick(View v){

Toast toast=Toast.makeText(MyHobby.this, "你选择了:"+getResult(), Toast.LENGTH_SHORT);

toast.show();

}

});

// 定义一个有名字的监听器类。之所以不用匿名内部类形式,是因为有多个控件都要使用这同一个监听器

OnCheckedChangeListener listener=new OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

Toast toast=Toast.makeText(MyHobby.this, "你选择了:"+getResult(), Toast.LENGTH_SHORT);

toast.show();

}

};

hobby1.setOnCheckedChangeListener(listener);

hobby2.setOnCheckedChangeListener(listener);

hobby3.setOnCheckedChangeListener(listener);

}

// 获取多选项中被勾选的结果。利用isChecked()方法来判断哪个选项被勾选

private String getResult(){

StringBuilder sb=new StringBuilder();

if(hobby1.isChecked()){

sb.append(hobby1.getText());

}

if(hobby2.isChecked()){

sb.append(hobby2.getText());

}

if(hobby3.isChecked()){

sb.append(hobby3.getText());

}

return sb.toString();

}

}

2.checkhobby.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="20sp"

android:text="爱好:" />

android:id="@+id/checkBox1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="游泳" />

android:id="@+id/checkBox2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="唱歌" />

android:id="@+id/checkBox3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="阅读" />

android:id="@+id/submit"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="提交" />

3.具体问题

虚拟装置管理器点击这个项目的提交按钮,并没有出现预期的Toast语句

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值