【问题标题】:Add New View to Layout Upon Button Click in Android在Android中单击按钮时向布局添加新视图
【发布时间】:2014-10-22 00:04:54
【问题描述】:

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

在每只手结束时单击按钮,我想将有关手的一些信息存储到 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 
            ...

谢谢! -K.H.

【问题讨论】:

    标签: android view


    【解决方案1】:

    以下代码在点击时添加文本视图:

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

    【讨论】:

    • 这很有趣。它适用于一键,但只有一键。 gamehistory = (LinearLayout) findViewById(R.id.gamehistory); layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); TextView handHistory = new TextView(ScoreboardActivity.this); handHistory.setText("测试"); gamehistory.addView(handHistory, layoutParams);
    • 我确定它只是不断覆盖旧视图。
    • 不是覆盖。你使用 LayoutParams.FILL_PARENT 而不是使用 LayoutParams.WRAP_CONTENT
    • 但是 Fill_PARENT 是宽度,这是我想要的。我使用 WRAP_CONTENT 作为高度,以便可以在其下方添加更多 TextView。这是我的 XML: 我正在尝试添加到 ScrollView 内的 LinearLayout “gamehistory”。
    • P.S.不知道为什么评论框显示不正常。
    【解决方案2】:

    使用以下代码。

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

    【讨论】:

    • 我认为这与我解决我一直遇到的其他问题的特殊方式有关。也许你可以帮助我:我有两个不同布局的按钮。我在一个记分板活动中的布局之间切换。我是否可以在活动的 onCreate() 方法中为他们两个定义 onClickListeners?似乎我不能,所以每当我使用 setContentView 切换布局时,我都必须激活正确的点击监听器。这意味着我每次都设置点击监听器,我认为这就是我“覆盖”我的 addView 的原因。
    • 好的,我换个说法。在一个名为 ScoreboardActivity 的活动中,我在 scoreboard_layout.xml 和 results_layout.xml 这两个布局之间切换,以便下注然后获得结果。 scoreboard_layout.xml 有一个名为“锁定投注”的按钮,results_layout.xml 有一个名为“锁定结果”的按钮。但是我不能在 onCreate() 中为 ScoreboardActivity 设置他们的两个点击监听器,因为显然你只能为当前内容视图 setContentView(...) 中的按钮设置一个点击监听器。所以我必须在递归中来回切换重置点击监听器。
    • 这可能与为什么我不能让新的 TextView 不覆盖旧的而不是添加的原因有关吗?
    • 我已将上面的代码大纲添加为“答案”,并展示了我如何使用您在 saveHandHistory() 中提供的内容
    • 为什么不使用不同的活动来进行结果布局和记分牌布局?
    【解决方案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 <------
                        }
                    }
                });
            }
        });
    }
    

    基于你给我的

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-28
      • 1970-01-01
      • 2011-10-19
      • 1970-01-01
      • 2018-05-15
      • 2011-09-03
      • 2019-11-20
      相关资源
      最近更新 更多