如何使片段中的按钮在单独的活动中操作整数变量?

时间:2014-02-05 06:10:55

标签: java android android-fragments

这是一个基本的Android应用程序,作为裁判罢工/球/出柜台。 MainActivity的操作栏中有一个设置图标。当“点击”此图标时,将启动一个新的活动,其中包含一个PreferenceFragment,其中包含一个checkboxpreference,以及一个由3个按钮组成的Fragment。这些按钮将stike_count,ball_count和total_outs_count重置为零。我花了一天时间在这上面,并且我很难弄清楚如何在单独的Activity的Fragment中按钮点击操作MainActivity中的整数变量。请指出我正确的方向。

MyApplication class
////////////////////////////////////////////////

package edu.umkc.baldwin;

import android.app.Application;

public class MyApplication extends Application {

    private int strikes, balls, outs;

    @Override
    public void onCreate() {
        super.onCreate();

        strikes = 0;
        balls = 0;
        outs = 0;
    }

    public void setStrikeCount(int strike_c){
        this.strikes = strike_c;
    }
    public void setBallCount(int ball_c){
        this.balls = ball_c;
    }
    public void setOutsCount(int out_c){
        this.outs = out_c;
    }
    public int getStrikeCount(){
        return strikes;
    }
    public int getBallCount(){
        return balls;
    }
    public int getOutCount(){
        return outs;
    }

}


MainActivity
//////////////////////////////////////////////////////////


package edu.umkc.baldwin;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/*
 *   MainAcitvity extends the Activity class and provides all the functionality
 *   of the homescreen (main_page_layout) widgets including the actionbar items and presumably the 
 *   menu button on the bottom left of the device (on most devices). 
 */
public class MainActivity extends Activity {

    private static final String TAG = "UmpireActivity";

    MyApplication app = (MyApplication) getApplication();

    TextView strikeCounterTV;
    TextView ballCounterTV;
    TextView totalOutCounterTV;

    private Button strikeCounterButton;
    private Button ballCounterButton;

    private int strike_count;
    private int ball_count;
    private int out_count;

    /*
     *    Method to update the TextViews representing balls,
     *    outs and strikes
     */
    public void updateViews(){
        strikeCounterTV.setText(String.valueOf(strike_count));
        ballCounterTV.setText(String.valueOf(ball_count));
        totalOutCounterTV.setText(String.valueOf(out_count));
    }

    /*
     *    Method to display a Toast message to the user when 
     *    strikes have reached 3 or balls have reached 4
     */
    private void displayToast(boolean x){
        int messageId = 0;
        if (x == true){
            messageId = R.string.strike_toast_view;
        } else {
            messageId = R.string.ball_toast_view;
        }
        Toast toast = Toast.makeText(MainActivity.this, messageId, Toast.LENGTH_SHORT);
        LinearLayout toastLayout = (LinearLayout) toast.getView();
        TextView toastTV = (TextView) toastLayout.getChildAt(0);
        toast.setGravity(Gravity.CENTER|Gravity.CENTER, 0, -150);
        toastTV.setTextSize(42);
        toast.show();
    }

    /*
     * (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "OnCreate(Bundle) called");
        setContentView(R.layout.main_page_layout);

        strikeCounterTV = (TextView) findViewById(R.id.strikeCountTextView);
        ballCounterTV = (TextView) findViewById(R.id.ballCountTextView);
        totalOutCounterTV = (TextView) findViewById(R.id.totalOutsTextViewCounter);

        strikeCounterButton = (Button) findViewById(R.id.strikeCountButton);
        ballCounterButton = (Button) findViewById(R.id.ballCountButton);

        strikeCounterButton.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {

                if (strike_count < 2){
                    strike_count++;
                    updateViews();
                } else {                   
                    // batter has reached strike limit
                    displayToast(true);
                    out_count++;
                    strike_count = 0;
                    ball_count = 0;
                    updateViews();
                }
            }
        });
        ballCounterButton.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                if (ball_count < 3){
                    ball_count++;
                    updateViews();
                } else {                   
                    // batter has reached ball limit
                    displayToast(false);
                    strike_count = 0;
                    ball_count = 0;
                    updateViews();
                }
            }
        });

        // http://developer.android.com/training/basics/data-storage/shared-preferences.html
        SharedPreferences savedObject = getPreferences(Context.MODE_PRIVATE);
        out_count = savedObject.getInt("outs_key", 0);
        strike_count = savedObject.getInt("strikes_key", 0);
        ball_count = savedObject.getInt("balls_key", 0);
        updateViews();
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        Log.d(TAG, "onSaveInstanceState() called");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //Displays actionbar items in app actionbar
        getMenuInflater().inflate(R.menu.actionbar_layout, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        super.onOptionsItemSelected(item);
        switch (item.getItemId()){
            case (R.id.about_menu_option): 
                Intent i = new Intent(this, About.class);
                startActivity(i);
                return true;
            case (R.id.reset_option):
                strike_count = 0;
                ball_count = 0;
                updateViews();
                return true;
            case (R.id.settings_menu_option):
                Intent j = new Intent(this, SettingsActivity.class);
                startActivity(j);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "onStart() called");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG, "onPause() called");

        app.setStrikeCount(strike_count);

        // SHOULD THIS CODE BE IN onPause()
        // OR onStop()  ??????????????????????????????????????????????????????
                // store value of out_count to restore after application exit
                // http://developer.android.com/training/basics/data-storage/shared-preferences.html
                SharedPreferences out_file = getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = out_file.edit();
                editor.putInt("outs_key", out_count);
                editor.putInt("strikes_key", strike_count);
                editor.putInt("balls_key", ball_count);
                editor.commit();
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume() called");
        // Save 'totalOuts' variable through application exit
        // http://developer.android.com/training/basics/data-storage/shared-preferences.html
        SharedPreferences savedObject = getPreferences(Context.MODE_PRIVATE);
        out_count = savedObject.getInt("outs_key", 0);
        strike_count = savedObject.getInt("strikes_key", 0);
        ball_count = savedObject.getInt("balls_key", 0);
        updateViews();
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d(TAG, "onStop() called");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy() called");
    }
}











SettingsActivity
/////////////////////////////////////////////////////////////////////

package edu.umkc.baldwin;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;

public class SettingsActivity extends Activity {

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

        FragmentManager ttsFragmentManager = getFragmentManager();
        FragmentTransaction ttsFragmentTransaction = ttsFragmentManager.beginTransaction();
        EnableTTSPreferenceFragment ttsFragment = new EnableTTSPreferenceFragment();
        ttsFragmentTransaction.add(R.id.tts_fragment, ttsFragment);
        ttsFragmentTransaction.commit();

        FragmentManager resetFragmentManager = getFragmentManager();
        FragmentTransaction resetFragmentTransaction = resetFragmentManager.beginTransaction();
        ResetFragment resetFragment = new ResetFragment();
        resetFragmentTransaction.add(R.id.reset_fragment, resetFragment);
        resetFragmentTransaction.commit();
    }
}


ResetFragment
///////////////////////////////////////////////////////////////

package edu.umkc.baldwin;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ResetFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View resetFragment = inflater.inflate(R.layout.reset_layout, container, false); 
        return resetFragment;
    }
}


EnableTTSPreferenceFragment
//////////////////////////////////////////////////////////////////////

package edu.umkc.baldwin;  
import android.os.Bundle;
import android.preference.PreferenceFragment;

public class EnableTTSPreferenceFragment extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.tts_preference_fragment);  
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);    
    }   
}


Android manifest file
///////////////////////////////////////////////////////////////////

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="edu.umkc.baldwin"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/umpire_buddy_icon"
        android:name="edu.umkc.baldwin.MyApplication"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="edu.umkc.baldwin.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>  
        </activity>

        <activity
            android:name="edu.umkc.baldwin.About"
            android:parentActivityName="edu.umkc.baldwin.MainActivity"
            android:label="@string/about_menu_text" >
        </activity>

        <activity
            android:name="edu.umkc.baldwin.Settings"
            android:parentActivityName="edu.umkc.baldwin.MainActivity"
            android:label="@string/settings_text" >
        </activity>

        <activity
            android:name="edu.umkc.baldwin.SettingsActivity"
            android:parentActivityName="edu.umkc.baldwin.MainActivity"
            android:label="@string/settings_text" >
        </activity>

    </application>

</manifest>

3 个答案:

答案 0 :(得分:0)

  1. 扩展Application对象并保存其中的警示计数值。所有活动都可以访问它。从设置更新它,主要活动也将能够看到更改。
  2. 当您开始设置活动时,请执行startActivityForResult并使设置活动返回结果,您可以使用该结果更新计数。

答案 1 :(得分:0)

AFAIK Fragments位于特定Activity的上下文中,中的公共方法可以像getActivity().methodName()一样访问。

现在如果你想要从片段中更改任何必须更改的数据,那么你可以采用以下两种方法中的任何一种:

  • fragment调用您的公共方法activity并在该公开方法中,您可以获得MainActivity的上下文并更改其公共变量。
  • 我不确定您的应用程序的设计或流程,但会建议创建静态变量并轻松地使用类名访问和修改它们

答案 2 :(得分:0)

使用Application类来解决这个问题。以下是此解决方案的示例代码:

<强> 1。创建应用程序类。

public class MyApplication extends Application {

    private int strike_count, ball_count, out_count;

    @Override
    public void onCreate() {
        super.onCreate();

        //initialize the variables here
        strike_count = 0;
        ball_count = 0;
        out_count = 0;
    }

    // create set and get functions

    public void setStrikeCount(int strike_count) {
        this.strike_count = strike_count;
    }

    public void setBallCount(int ball_count) {
        this.ball_count = ball_count;
    }

    public void setOutCount(int out_count) {
        this.out_count = out_count;
    }

    public int getStrikeCount() {
        return strike_count;
    }

    public int getBallCount() {
        return ball_count;
    }

    public int getOutCount() {
        return out_count;
    }
}

<强> 2。在清单文件中提及此Application类。

<application android:name="yourpackagename.MyApplication"/>

第3。使用以下代码访问任何活动中的应用程序对象。

MyApplication app = (MyApplication) getApplication();

完成此操作后,您可以通过简单地使用任何应用程序的公共函数来操纵任何活动中的这些变量,例如:

app.setStrikeCount(10);