1

好的,所以我试图将一个按钮/视图添加到一个新的活动,而不是 Viewgroup 已经在其中的活动。这是我到目前为止的代码:

源:

public class Main extends Activity {
    Button btn, btn1;
    LayoutInflater linflater;
    LinearLayout l;
    static int pos = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button  ButtonBack = (Button) findViewById(R.id.dynamicoption);
        ButtonBack.setOnClickListener(new View.OnClickListener(){

              public void onClick(View v) {
                  // Perform action on click

                startActivity(new Intent("com.nour.ImamKhomeini.DYNAMICOPTION"));


              }

          });

        btn = (Button) findViewById(R.id.Button01);

        l = (LinearLayout) findViewById(R.id.LinearLayout01);
        linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        btn.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
              View myView = linflater.inflate(R.layout.dynamicoption, l, true);
              myView.setId(pos);
              pos++;
          }
        });

        btn = (Button) findViewById(R.id.Button01);
        btn1 = (Button) findViewById(R.id.Button02);
        l = (LinearLayout) findViewById(R.id.LinearLayout01);
        linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        btn.setOnClickListener(new myListener());
        btn1.setOnClickListener(new myListener1());
    }

    class myListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            View myView = linflater.inflate(R.layout.dynamicoption, null);
            myView.setId(pos);
            pos++;
            l.addView(myView);
        }
    }

    class myListener1 implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if (pos != 0) {
                pos--;
                View myView = l.findViewById(pos);
                l.removeView(myView);
            }
        }
    }
}

这是我的xml:

主要的:

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

    <Button android:id="@+id/Button01" android:background="@drawable/ic_launcher"
        android:layout_height="45dip" android:layout_width="45dip"></Button>

    <Button android:id="@+id/Button02" android:background="@drawable/ic_launcher"
        android:layout_height="45dip" android:layout_width="45dip"></Button>

    <LinearLayout android:id="@+id/LinearLayout01"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:orientation="vertical"></LinearLayout>
</LinearLayout>

动态选项.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:orientation="horizontal">
    <EditText android:text="@+id/EditText01" android:id="@+id/EditText01"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>
    <Button android:text="@+id/Button01" android:id="@+id/Button01"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

任何帮助表示赞赏。谢谢你。

4

1 回答 1

1

我会做一些改变。看我如何创建 OnClickListener,你不需要创建新的子类。并注意我如何使用布局充气器:

btn.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        View myView = linflater.inflate(R.layout.dynamicoption, l, true);
        myView.setId(pos);
        pos++;
    }
});

此外,当 XML 开始和结束标签之间没有任何内容时:

<EditText 
    android:id="@+id/EditText01"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:text="@+id/EditText01" ></EditText>

您可以使用/>速记并节省一些打字时间

<EditText 
    android:id="@+id/EditText01"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:text="@+id/EditText01" />

这是您的 onCreate() 方法的外观:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button ButtonBack = (Button) findViewById(R.id.dynamicoption);
    ButtonBack.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            // Perform action on click
            startActivity(new Intent("com.nour.ImamKhomeini.DYNAMICOPTION"));
        }
    });

    l = (LinearLayout) findViewById(R.id.LinearLayout01);
    linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    btn = (Button) findViewById(R.id.Button01);
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            View myView = linflater.inflate(R.layout.dynamicoption, l, true);
            myView.setId(pos);
            pos++;
        }
    });

    btn1 = (Button) findViewById(R.id.Button02);
    btn1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (pos > 0) {
                pos--;
                l.removeView(l.findViewById(pos));
            }
        }
    });
}
于 2012-08-22T18:22:34.770 回答