在 Android 中单击按钮后将新视图添加到布局

发布于 2024-10-13 02:05:32 字数 583 浏览 1 评论 0原文

我正在为纸牌游戏“黑桃”开发记分应用程序。

单击每只手牌末尾的按钮后,我想将有关该手牌的一些信息存储到 TextView 中,并在 ScrollView 中显示所有手牌历史记录。

如何修改 .XML 布局或以其他方式将新的 TextView 添加到代码中的布局?

我已经尝试过...

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 
            ...

谢谢! -KH

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江心雾 2024-10-20 02:05:32

以下代码添加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); 

            }
        });
    }
}

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

            }
        });
    }
}
奢望 2024-10-20 02:05:32

使用以下代码。

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

        });
    }
}

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

        });
    }
}
信愁 2024-10-20 02:05:32
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 <------
                    }
                }
            });
        }
    });
}

根据你给我的

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);
}
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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文