如何使用java代码而不是xml代码将按钮添加到android studio中的另一个活动中



我想从1个活动中添加一个按钮-">pop_accompanients.xml"到另一个活动-">pop_cart.xml";,使用java代码。为此,我创建了类"的实例pop_cart.java"在">pop_accompanients.java";,调用函数";cart_button_creator";其在">pop_cart.java";。但我的应用程序一打开就崩溃了。

pop_accompanients.java

package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Random;
public class pop_accompaniments extends AppCompatActivity implements View.OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pop_accompaniments);
Button assorted_condiments = findViewById(R.id.assorted_condiments);
}
@Override
public void onClick(View v) {
setContentView(R.layout.pop_cart);
pop_cart main = new pop_cart();
int id;
String name;
Button myButton;
switch (v.getId()) {
case R.id.assorted_condiments:
myButton = new Button(this);
name = "Assorted Condiments";
id = generateButtonId();
main.cart_content_keeper(id, name, myButton);
}
}
}

pop_cart.java

package com.example.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
public class pop_cart extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.pop_cart);
}
public void cart_button_creator(String name, int id, Button myButton) {
//setContentView(R.layout.pop_cart);
LinearLayout ll = findViewById(R.id.cart_items);
myButton.setText(name);
myButton.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
myButton.setId(id);
ll.addView(myButton);
}

public void cart_content_keeper(int id, String name, final Button myButton) {
int n = 0;
int[] id_arr = {};
String[] name_arr = {};
//int price = 0;
name_arr = add_item(n, name_arr, name);
id_arr = add_id(n, id_arr, id);
n = n + 1;
Button refresh = findViewById(R.id.refresh);
final int finalN = n;
final String[] finalName_arr = name_arr;
final int[] finalId_arr = id_arr;
refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.refresh) {
for (int i = 0; i < finalN; i++) {
cart_button_creator(finalName_arr[i], finalId_arr[i], myButton);
}
}
}
});
}

public static String[] add_item(int n, String[] arr, String x)
{
int i;
String[] newarr = new String[n + 1];
for (i = 0; i < n; i++)
newarr[i] = arr[i];
newarr[n] = x;
return newarr;
}

public static int[] add_id(int n, int[] arr, int x)
{
int i;
int[] newarr = new int[n + 1];
for (i = 0; i < n; i++)
newarr[i] = arr[i];
newarr[n] = x;
return newarr;
}
}

很抱歉有这么长的代码,但我已经尝试了几乎所有可能的方法,从另一个类的实例调用func,以不同的方式传递变量,尝试将整个变量生成器作为函数调用,并在pop_accompanients.java中仅将视图v作为参数传递,但当我点击按钮";组合条件";在第一步,或者如果我刚启动应用程序,它就会崩溃。

此外,如果我在相同的活动中创建按钮,则按钮创建得很好pop_accompanients.xml"同时从"调用button_retor方法pop_accompanients.java";。

但我希望它们在CART活动中显示,而不是在ACCOMPANMENTS活动中显示。

我猜您需要在活动pop_cart.java处于前台时创建按钮。既然如此,为什么不直接执行pop_cart.java的onCreate((中的按钮代码呢?

如果您想在pop_accompanients.java中决定是否创建按钮,可以通过intent将布尔值从pop_accoCompanients.java传递到pop_cart.java!

在pop_accompaniements.java中:

Intent intent = new Intent(this, pop_cart.class);
intent.putExtra("someBool", true);

在pop_cart.java中:

@Override
protected void onCreate(Bundle savedInstanceState) {
Boolean bool = getIntent().getExtras().getBoolean("someBool");
//Now you can check if the bool is true or false
if(bool){
//Execute button code here
}
}

相关内容

  • 没有找到相关文章

最新更新