Add New View to Layout Upon Button Click in Androi

2020-07-23 03:08发布

问题:

I'm developing a Scorekeeping Application for the card game "Spades".

Upon a button click at the end of each hand, I would like to store some information about the hand into a TextView and display all of the hand histories in a ScrollView.

How can I modify an .XML layout or otherwise add a new TextView to a layout in code?

I have tried...

public static LinearLayout gamehistory;

gamehistory = (LinearLayout) findViewById(R.id.gamehistory);


public void setClickListener1(){

lockwagersButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

setContentView(R.layout.results_layout);

gameHistory.addView(new TextView(...));       //  I can't get this to work 
            ...

Thanks! -K.H.

回答1:

following code add textview onclick:

public class ViewOnClick extends Activity {
    LinearLayout.LayoutParams layoutParams;
    LinearLayout ll;
    static int i;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button b = (Button)findViewById(R.id.Button01);
        ll = (LinearLayout)findViewById(R.id.ll);
        layoutParams = new LinearLayout.LayoutParams
        (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        b.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                TextView view = new TextView(ViewOnClick.this);             
                view.setText(++i+" view");
                ll.addView(view, layoutParams); 

            }
        });
    }
}


回答2:

Use the following code.

   <?xml version="1.0" encoding="utf-8"?>
       <RelativeLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent">
        <Button 
            android:text="Button01" 
            android:id="@+id/Button01" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"></Button>  
        <ScrollView
            android:layout_below="@id/Button01"
            android:id="@+id/scrollview" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"> 
            <LinearLayout 
                android:id="@+id/gamehistory" 
                android:orientation="vertical" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"> 
            </LinearLayout> 

        </ScrollView>
</RelativeLayout>
public class Scrollview1 extends Activity {
    ScrollView scrollview;
    LinearLayout  linearLayout;
    LinearLayout.LayoutParams layoutParams;
    static int i;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        scrollview = (ScrollView)findViewById(R.id.scrollview);
        linearLayout = (LinearLayout)findViewById(R.id.gamehistory);
        Button b = (Button)findViewById(R.id.Button01);
        layoutParams = new LinearLayout.LayoutParams
        (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        b.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
                TextView view = new TextView(Scrollview1.this);             
                view.setText(++i+" view");
                linearLayout.addView(view, layoutParams); 
            }

        });
    }
}


回答3:

public void setClickListeners() 
{
    lockwagersButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
         // Do Stuff
        lockresultsButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                        calculateScores();                  
                        setContentView(R.layout.scoreboard_layout);
                        SettingsActivity.currentdealer.getNextDealer();
                        setSpinners1();
                        findDisplays();
                        setPlayerNamesScoreboard();
                        lockwagersButton = (Button) findViewById(R.id.lockwagers);
                        displayScores();
                        checkForWinner();
                        saveHandHistory();
                        setClickListeners(); // This is the recursion <------
                    }
                }
            });
        }
    });
}

Based on what you gave me

public void saveHandHistory(){
    scrollview = (ScrollView) findViewById(R.id.ScrollView1);
    gamehistory = (LinearLayout) findViewById(R.id.gamehistory); 
    layoutparams = new LinearLayout.LayoutParams (LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    TextView handHistory = new TextView(ScoreboardActivity.this);
    handHistory.setText("Hand History Will Go Here");
    gamehistory.addView(handHistory, layoutparams);
}


标签: android view