简体   繁体   中英

How to change Fragment in Android

I am learning Android and stuck at this. I want to make a project in Android. In MainActivity I have a button and want to set onClick() event such that pressing it, if button's text is "SOLVE" then add fragment Output and change button's name to "BACK" and if button's text is "BACK" then add fragment Input and change Button's text to "SOLVE".

Here's my MainActivity.java code

package com.example.sudokusolver;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {
Button a        
String check ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        a=(Button) findViewById(R.id.press);
        check=a.getText().toString();


        a.setOnClickListener(new View.OnClickListener(){
             public void onClick(View v){

                if(check.equals("SOLVE")){

                     Output OP=new Output();

                     FragmentManager fragmentManager = getFragmentManager();
                     FragmentTransaction ft =
                     fragmentManager.beginTransaction();

                     ft.add(R.id.frag,OP);

                     ft.commit();

                     a.setText("BACK");
                }
                else
                {
                    Input IP=new Input();

                     FragmentManager fragmentManager = getFragmentManager();
                     FragmentTransaction ft =
                     fragmentManager.beginTransaction();

                     ft.add(R.id.frag,IP);

                     ft.commit();

                     a.setText("SOLVE");

                }

             }
        });

    }

}

here's main_activit.xml file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">


    <FrameLayout 
        android:id="@+id/frag"
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        />

    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="90dp">

        <Button
            android:id="@+id/press"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="@string/show" />

    </RelativeLayout>

</LinearLayout>

The problem is (you might have seen) I can't compare ( check string) non-final variable inside an inner class. SO please help me with this and if you think my whole idea is wrong then suggest me proper way.

(if other files require then tell me, I will edit)

EDIT

There is something wrong with my logic. when I first press 'Solve' it turned to 'Back'. But when I press 'Back' it doesn't turn to 'Solve'. I want Solve > Back > Solve > Back... am i missing something? (maybe in add/replace transaction ?)

Replace if(check.equals("SOLVE")) with if(((Button)v).getText().toString().equals("SOLVE")) .

To set a new text use this ((Button)v).setText("YourText")

In OnClick the view is getting passed as the argument is the Button itself. So you can manipulate the view as Button and set or get its properties

To be able to access variable a (your Button) inside of anonymous classes created in some method, you need to declare the variable final. Example:

final Button a = (Button) findViewById(R.id.press);

Make check (your String) final too, as it's used in the anonymous class also:

final String check = a.getText().toString();

Define your button as class variable as

public class MainActivity extends ActionBarActivity {
Button a;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    a =(Button) findViewById(R.id.press);

and compare in listener as

if(a.getText().toString().equals("SOLVE")){

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM