我的Toast消息未正确显示

时间:2014-02-11 07:35:14

标签: android radio-button toast

我正在制作一个Android程序,使用Toast messages

显示RadioButton

在我的计划中:

Toast.makeText(MainActivity.this, rd.toString(), Toast.LENGTH_LONG).show(); //here i'm using "rd.toString()", I tried to be used "rd.getText()" but it's showing an error.

现在显示它显示this错误。

这是我显示单选按钮的完整源代码,请帮忙解决这个问题?

MainActivity.java:

package com.example.lesson08;

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.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity {

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

        addListener();
    }

    public void addListener() {
        final RadioGroup rd=(RadioGroup)findViewById(R.id.sex);
        Button disp=(Button)findViewById(R.id.display);
        disp.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                int selectID=rd.getCheckedRadioButtonId();

                RadioButton rb=(RadioButton)findViewById(selectID);
                Toast.makeText(MainActivity.this, rd.toString(), Toast.LENGTH_LONG).show();
            }
        });
    }    
}

logcat的:

02-11 07:20:22.358: I/Choreographer(1575): Skipped 30 frames!  The application may be doing too much work on its main thread.
02-11 07:20:24.778: D/libEGL(1575): loaded /system/lib/egl/libEGL_genymotion.so
02-11 07:20:24.870: D/(1575): HostConnection::get() New Host Connection established 0xb7daa1e8, tid 1575
02-11 07:20:24.918: D/libEGL(1575): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
02-11 07:20:24.962: D/libEGL(1575): loaded /system/lib/egl/libGLESv2_genymotion.so
02-11 07:20:25.202: W/EGL_genymotion(1575): eglSurfaceAttrib not implemented
02-11 07:20:25.242: E/OpenGLRenderer(1575): Getting MAX_TEXTURE_SIZE from GradienCache
02-11 07:20:25.242: E/OpenGLRenderer(1575): Getting MAX_TEXTURE_SIZE from GradienCache
02-11 07:20:25.662: E/OpenGLRenderer(1575): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
02-11 07:20:25.666: D/OpenGLRenderer(1575): Enabling debug mode 0
02-11 07:20:25.918: D/dalvikvm(1575): GC_FOR_ALLOC freed 54K, 4% free 2926K/3044K, paused 15ms, total 17ms
02-11 07:20:57.814: D/libEGL(1634): loaded /system/lib/egl/libEGL_genymotion.so
02-11 07:20:57.910: D/(1634): HostConnection::get() New Host Connection established 0xb7d8c188, tid 1634
02-11 07:20:58.002: D/libEGL(1634): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
02-11 07:20:58.058: D/libEGL(1634): loaded /system/lib/egl/libGLESv2_genymotion.so
02-11 07:20:58.290: W/EGL_genymotion(1634): eglSurfaceAttrib not implemented
02-11 07:20:58.298: E/OpenGLRenderer(1634): Getting MAX_TEXTURE_SIZE from GradienCache
02-11 07:20:59.006: E/OpenGLRenderer(1634): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
02-11 07:20:59.010: D/OpenGLRenderer(1634): Enabling debug mode 0
02-11 07:20:59.362: D/dalvikvm(1634): GC_FOR_ALLOC freed 61K, 5% free 2919K/3044K, paused 14ms, total 17ms
02-11 07:22:59.346: D/libEGL(1712): loaded /system/lib/egl/libEGL_genymotion.so
02-11 07:22:59.370: D/(1712): HostConnection::get() New Host Connection established 0xb7da6fa8, tid 1712
02-11 07:22:59.462: D/libEGL(1712): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
02-11 07:22:59.466: D/libEGL(1712): loaded /system/lib/egl/libGLESv2_genymotion.so
02-11 07:22:59.938: W/EGL_genymotion(1712): eglSurfaceAttrib not implemented
02-11 07:23:01.170: E/OpenGLRenderer(1712): Getting MAX_TEXTURE_SIZE from GradienCache
02-11 07:23:01.502: E/OpenGLRenderer(1712): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
02-11 07:23:01.518: D/OpenGLRenderer(1712): Enabling debug mode 0
02-11 07:23:02.314: D/dalvikvm(1712): GC_FOR_ALLOC freed 67K, 5% free 2899K/3032K, paused 75ms, total 77ms

4 个答案:

答案 0 :(得分:0)

像这样使用:

RadioButton rb=(RadioButton)findViewById(selectID);
Toast.makeText(MainActivity.this, rb.getText().toString(), Toast.LENGTH_LONG).show();

rb.toString()返回无线电组实例的名称。如果您先使用getText()。它返回文本并将其转换为字符串。

答案 1 :(得分:0)

我想您要显示已选中的RadioButton文字。所以你可以使用以下代码。

RadioButton selectedRadioButton=(RadioButton)findViewById(selectID);
Toast.makeText(MainActivity.this, selectedRadioButton.getText().toString(), Toast.LENGTH_LONG).show();

答案 2 :(得分:0)

像这样改变

RadioButton rb=(RadioButton)findViewById(selectID);
Toast.makeText(MainActivity.this, rb.getText().toString(), Toast.LENGTH_LONG).show();

代替这个

RadioButton rb=(RadioButton)findViewById(selectID);
Toast.makeText(MainActivity.this, rd.toString(), Toast.LENGTH_LONG).show();

答案 3 :(得分:0)

使用:

RadioButton rb=(RadioButton)findViewById(selectID);
                Toast.makeText(MainActivity.this, rb.getText().toString(), Toast.LENGTH_LONG).show();