Pregunta

I have two class , one is public class range extends LinearLayout.

Another one is public class Main extends Activity

In the Main , I use MyWindowManager.createBigWindow(getApplicationContext()); to call the range class.

The range class:

public class Out_of_range extends LinearLayout {

    public static int viewWidth;
    public static int viewHeight;

    public Out_of_range(final Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        LayoutInflater.from(context).inflate(R.layout.out_of_range, this);
        View view = findViewById(R.id.big_window_layout);
        viewWidth = view.getLayoutParams().width;
        viewHeight = view.getLayoutParams().height;


        TextView text = (TextView)findViewById(R.id.text);
        text.setText("loss :"+ Main.tempAddress);

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

        back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                    // TODO Auto-generated method stub
/*----------------------------------------------------------------------------------------------
                               //I want to start Activity when I click the button here.

                    final Intent mainintent = new Intent(getContext(), Main.class);
                startActivity(mainintent);

*/-----------------------------------------------------------------------------------------------
                }
            });

        }

And I want to start Activity from a class( this class extends LinearLayout )

I use intent , but it has error.

The method startActivity(Intent) is undefined for the type new View.OnClickListener(){}

¿Fue útil?

Solución

you could make your method createBigWindow return the range object it just created

public static Out_of_range createBigWindow(Context context) {
    WindowManager windowManager = getWindowManager(context);
    int screenWidth = windowManager.getDefaultDisplay().getWidth();
    int screenHeight = windowManager.getDefaultDisplay().getHeight();
    if(bigWindow == null) {
        bigWindow = new Out_of_range(context);
        if(bigWindowParams == null) {
            bigWindowParams = new LayoutParams();
            bigWindowParams.x = screenWidth / 2 - Out_of_range.viewWidth / 2;
            bigWindowParams.y = screenHeight / 2 - Out_of_range.viewHeight / 2;
            bigWindowParams.type = LayoutParams.TYPE_PHONE;
            bigWindowParams.format = PixelFormat.RGBA_8888;
            bigWindowParams.gravity = Gravity.LEFT | Gravity.TOP;
            bigWindowParams.width = Out_of_range.viewWidth;
            bigWindowParams.height = Out_of_range.viewHeight;

        }
        windowManager.addView(bigWindow, bigWindowParams);
    }

    return bigWindow;
}

and then in your Out_of_range class create a method to receive the String you want to pass.

Edit:

//in your Out_of_range class

public void receiveStringValue(String value) {
  // do whatever you want 
}

and use it from your Main class after using the createBigWindow method:

Out_of_range range = MyWindowManager.createBigWindow(this);
range.receiveStringValue(yourString);

I didn't try this out but I think it's worth trying.

Edit2:

Now that you've updated your Question it's clearer: try this:

public class Out_of_range extends LinearLayout {

public static int viewWidth;
public static int viewHeight;

public Out_of_range(final Context context, String value) {
    super(context);
    // TODO Auto-generated constructor stub
    LayoutInflater.from(context).inflate(R.layout.out_of_range, this);
    View view = findViewById(R.id.big_window_layout);
    viewWidth = view.getLayoutParams().width;
    viewHeight = view.getLayoutParams().height;


    device = (TextView) findViewById(R.id.device);
    device.setText("device = " + value);


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

    back.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            MyWindowManager.removeBigWindow(context);
        }
    });

}

And in your main, do something like this:

MyWindowManager.createBigWindow(getApplicationContext(), "your value here");

Otros consejos

I define the String in Main.class like public static String tempName;

And in range class , use Main.tempName to get the value.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top