gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:26:59 25 4
gpt4 key购买 nike

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

另一个是public class Main extends Activity

Main 中,我使用 MyWindowManager.createBigWindow(getApplicationContext()); 来调用 range 类

范围类:

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 )

我使用了 intent ,但它有错误。

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

最佳答案

你可以让你的方法 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 类中创建一个方法来接收您要传递的字符串。

编辑:

//in your Out_of_range class

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

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

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

我没有尝试过,但我认为值得一试。

编辑2:

现在你已经更新了你的问题,它更清楚了:试试这个:

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

}

在你的 main 中,做这样的事情:

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

关于android - 如何从 Android 4.3 中的类(此类扩展 LinearLayout)启动 Activity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20092570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com