Android:Inflate XML OnClick?

时间:2013-06-23 23:12:41

标签: java android

Hello其他程序员!

在我的应用程序中,我希望下面列出的一些XML被夸大OnClick。它需要放入另一个LinearLayout.I还想将TextView中的文本和EditTexts的id更改为“ListItem”+ numOfItems。我怎么能这样做?

要夸大的XML:

<LinearLayout
                    android:paddingLeft="15dp"
                    android:weightSum="100"
                    android:layout_width="fill_parent"
                    android:layout_height="80dp"
                    android:orientation="horizontal">
                <TextView
                        android:layout_weight="10"
                        android:layout_width="10dp"
                        android:layout_height="80dp"
                        android:text="1."
                        android:id="@+id/tvItem1"/>
                <EditText
                        android:layout_weight="90"
                        android:layout_width="100px"
                        android:layout_height="80dp"
                        android:hint="List Item 1"
                        android:id="@+id/etItem1"
                        android:paddingTop="50px"/>
            </LinearLayout>

完整的java类:

package com.frostbytedev.randomgenie;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Steven on 6/23/13.
 */
public class Test extends Activity implements View.OnClickListener{
    java.util.List<TextView> listOfET = new ArrayList<TextView>();
    LinearLayout insideScroll;
    ScrollView svItems;
    TextView etItem1, etItem2;
    Button Add, Remove;
    int numOfItems = 2, width, height;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dynamictest);
        initialize();
    }

    private void initialize() {
        Add=(Button)findViewById(R.id.bAdd);
        Remove=(Button)findViewById(R.id.bRemove);
        insideScroll=(LinearLayout)findViewById(R.id.insideScroll);
        etItem1=(TextView)findViewById(R.id.etItem1);
        etItem2=(TextView)findViewById(R.id.etItem2);
        svItems=(ScrollView)findViewById(R.id.svItems);
        Add.setOnClickListener(this);
        Remove.setOnClickListener(this);

        listOfET.add(etItem1);
        listOfET.add(etItem2);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.bAdd:
                numOfItems += 1;
                LinearLayout layout = new LinearLayout(this);
                layout.setOrientation(LinearLayout.HORIZONTAL);
                layout.setPadding(getDP(15f), 0, 0, 0);
                layout.setLayoutParams(new LinearLayout.LayoutParams((getDP(80f)), LinearLayout.LayoutParams.FILL_PARENT));


                TextView numberView = new TextView(this);
                LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(getDP(80f),getDP(10f));
                numberView.setLayoutParams(tvParams);
                numberView.setText(numOfItems + ".");
                layout.addView(numberView);

                EditText optionText = new EditText(this);
                LinearLayout.LayoutParams etParams = new LinearLayout.LayoutParams(getDP(80f),getDP(100f));
                numberView.setLayoutParams(etParams);
                numberView.setHint("List Option "+numOfItems);
                layout.addView(optionText);

                insideScroll.addView(layout);

                break;
            case R.id. bRemove:

                listOfET.remove(numOfItems);
                numOfItems -= 1;
                break;

        }
    }

    private int getDP(float i) {
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        float dp = i;
        float fpixels = metrics.density * dp;
        int pixels = (int) (fpixels + 0.5f);
        return pixels;
    }

}

这是我的java类

1 个答案:

答案 0 :(得分:0)

将id添加到XML项目中.....

       <LinearLayout
                android:paddingLeft="15dp"
                android:weightSum="100"
                android:layout_width="fill_parent"
                android:layout_height="80dp"
                android:orientation="horizontal">
            <TextView
                    android:id="@+id/textViewId"
                    android:layout_weight="10"
                    android:layout_width="10dp"
                    android:layout_height="80dp"
                    android:text="1."
                    android:id="@+id/tvItem1"/>
            <EditText
                    android:id="@+id/editTextId"
                    android:layout_weight="90"
                    android:layout_width="100px"
                    android:layout_height="80dp"
                    android:hint="List Item 1"
                    android:id="@+id/etItem1"
                    android:paddingTop="50px"/>
        </LinearLayout>

....将此代码添加到listView onItemClickListener ....

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView <? > parent, View view,
        int position, long id) {


       LayoutInflater inflater = LayoutInflater.from(getBaseContext());
       View theInflatedView = inflater.inflate(R.layout.your_layout, null);
       //Get the textView in your xml to set the text in it
       TextView textView = theInflatedView.findViewById(R.id.textViewId);
       textView.setText("Text on the textView");
       //Get the EditText in you xml to set the new id 
       EditText editText = theInflatedView.findViewById(R.id.editTextId);
       editText.setId(position);
       //add the view into the LinearLayout container (global variable)
       insideScroll.addView(theInflatedView);

    }
});

如果您点击按钮将项目添加到LinearLayout,那么......

您的活动实现了OnClickListener接口....

public class MainActivity extends Activity implements OnClickListener {

setOnClickListener到你的按钮......

button.setOnClickListener(this);

现在您收到点击onClick方法....

@Override
public void onClick(View v) {

         switch(v.getId()){
           case R.id.buttonId:
               LayoutInflater inflater = LayoutInflater.from(this);
               View theInflatedView = inflater.inflate(R.layout.your_layout, null);
               //Get the textView in your xml to set the text in it
               TextView textView = theInflatedView.findViewById(R.id.textViewId);
               textView.setText("Text on the textView");
               //Get the EditText in you xml to set the new id 
               EditText editText = theInflatedView.findViewById(R.id.editTextId);
               editText.setId(linearLayout.getChildCount()+1);
               //add the view into the LinearLayout (global variable)
               insideScroll.addView(theInflatedView);
           break;
      }
}