0

嗨,我正在尝试开发一个在每个间隔时间运行的应用程序,假设每 1 分钟它会显示一些Toast message.

但问题是我使用RadioButton的功能是完美的,但是当我点击一个单选按钮时,它会是绿色的,但是当我关闭并重新打开活动时,我会因为没有选择任何单选按钮而得到。

这是我的MainActivity.java

public class MainActivity extends Activity {

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

public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radio_one_min:
            if (checked)
            {
                //some code
            }
            break;
        case R.id.radio_ten_min:
            if (checked)
            {
                //some code
            }  
            break;

        case R.id.radio_disable:
            if (checked)
            {
                //some code
            }  
            break;   
    }
}
}

这是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/radio">
<RadioButton android:id="@+id/radio_disable"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Disable"
    android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio_one_min"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="1 minute"
    android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio_ten_min"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="10 minute"
    android:onClick="onRadioButtonClicked"/>
</RadioGroup>

请帮我解开这个谜。

提前致谢...

4

3 回答 3

1

这是最简单的方法,根本不需要sharedpreference。使用它时你会感到困惑。像这样保持简单

public class MainActivity extends Activity {
Public static int flag=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(flag==1)
radio_one_min.setChecked(true);
else if(flag==2)
radio_ten_min.setCheckek(true);
else if(flag==3)
radio_disable.setCheckek(true);

}

public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();

// Check which radio button was clicked
switch(view.getId()) {
    case R.id.radio_one_min:
        if (checked)
        {
flag =1;
            //some code
        }
        break;
    case R.id.radio_ten_min:
        if (checked)
        {
  flag=2 ;             
 //some code
        }  
        break;

    case R.id.radio_disable:
        if (checked)
        {
 flag=3;
            //some code
        }  
        break;   
}
 }
}
于 2013-07-11T09:45:19.110 回答
1

此代码用于存储评分栏状态,当我们开始新活动时,您将看到之前的评分状态..

package com.example.ratingbar;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class RatingbarMainActivity extends Activity {

RatingBar ratingbarClick;
Button sub_btn;
TextView textRatingView , textRatingViewSave;
Boolean val = true;
float ans = (float) 0.0;
//--------------------------------------------------------------------------------------------
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ratingbar_main);

    ratingbarClick = (RatingBar) findViewById(R.id.ratingBar1);
    ratingbarClick.setOnRatingBarChangeListener(rateObj);


    SharedPreferences sharePref = PreferenceManager.getDefaultSharedPreferences
             (RatingbarMainActivity.this);

    ans = sharePref.getFloat("Get_Rating", 0.0f);
    System.out.println("--------------------------------------ans = " + ans);
    if(val) {
        ratingbarClick.setRating(ans);
    }
    else {
        ratingbarClick.setRating(ans);
    }

    textRatingView = (TextView) findViewById(R.id.ratingView);


}

//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------

    RatingBar.OnRatingBarChangeListener rateObj = new RatingBar.OnRatingBarChangeListener() {

        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser) {

            //textRatingView.setText(String.valueOf(rating));
            ans = ratingbarClick.getRating();

            SharedPreferences sharePref = PreferenceManager.getDefaultSharedPreferences
                            (RatingbarMainActivity.this);
            SharedPreferences.Editor edit = sharePref.edit();
            edit.putFloat("Get_Rating", ans);
            edit.commit();
            val = false;

        }
    };


//--------------------------------------------------------------------------------------------

    }

---------------------------------------------------------------------------------------------------
    activity_ratingbar_main.xml file 

<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" >

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/ratingBar1"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="23dp"
    android:text="Select Your Rating Bar Here"
    tools:context=".RatingbarMainActivity" />

<RatingBar
    android:id="@+id/ratingBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="21dp"
    android:layout_marginTop="63dp" />


<TextView
    android:id="@+id/ratingView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/ratingBar1"
    android:text="TextView" />

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:text="Click To Save Rating In TextBox" />

 </RelativeLayout>
于 2013-07-11T10:07:51.307 回答
0

[编辑]

我首先找到了 developer.android.com/training/basics/activity-lifecycle/recreating.html文件,所以我的第一个猜测是使用Bundleson Save/Resume Instace State方法。但是,这似乎效果不佳。

这是我对工作解决方案的最后尝试(使用SharedPreferences类,如一些用户所建议的那样):

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;

public class MainActivity extends Activity {

final String PREFERENCES = "prefs";
final String RADIO_BUTTON = "prefsval";

SharedPreferences sp;
Editor e;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sp = this.getSharedPreferences(PREFERENCES, MODE_PRIVATE);
    e = sp.edit();
}

@Override
protected void onResume() {
    super.onResume();
    if (sp != null) {
        if (sp.getInt(RADIO_BUTTON, 0) != 0) {
            RadioButton rb;
            rb = (RadioButton) findViewById(sp.getInt(RADIO_BUTTON, 0));
            rb.setChecked(true);
        }
    }
}

public void onRadioButtonClicked(View view) {
    e.putInt(RADIO_BUTTON, view.getId());
    e.apply();
}
}
于 2013-07-11T09:24:13.867 回答