如何从Android 4.3中的类(此类扩展LinearLayout)启动Activity?

时间:2013-11-20 09:54:39

标签: android android-linearlayout android-4.3-jelly-bean

我有两个班,一个是public class range extends LinearLayout

另一个是public class Main extends Activity

主要中,我使用MyWindowManager.createBigWindow(getApplicationContext());来调用范围类

范围类:

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);

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

        }

我想从一个类开始Activity(这个类扩展了LinearLayout)

我使用意图,但它有错误。

方法startActivity(Intent)未定义类型new View.OnClickListener(){}

2 个答案:

答案 0 :(得分:2)

你可以让你的方法createBigWindow返回它刚刚创建的范围对象

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;
}

然后在你的Out_of_range类中创建一个方法来接收你想要传递的String。

编辑:

//in your Out_of_range class

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

并在使用createBigWindow方法后从Main类中使用它:

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

我没试过,但我觉得值得尝试。

Edit2:

既然你已经更新了你的问题就更清楚了:试试这个:

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);
        }
    });

}

在你的主体,做这样的事情:

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

答案 1 :(得分:0)

我在Main.class中定义String,如public static String tempName;

在范围类中,使用Main.tempName来获取值。