android studio生成带有按钮的textview

时间:2015-04-27 19:47:18

标签: android android-studio

单击按钮时是否可以生成文本视图?

目前我有一个只获取按钮ID的按钮

 public void xxx(View v){
    System.out.println(v.getId());
    textView.setText("the button clicked was" + v.getId());
}

我是否有办法在每次点击按钮时生成文本视图?

在看了一些回复后,这是完整的,但它不起作用:

package com.example.earth.stacktest1;

import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

    public class SampleActivity extends MainActivity {

        int counter = 0;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);

            Button button = (Button) findViewById(R.id.add_button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    counter++;

                    TextView textView = new TextView(SampleActivity.this);
                    textView.setText("TextView " + counter);
                    textView.setBackgroundColor(Color.parseColor("#FF0000"));
                    textView.setTextColor(Color.parseColor("#00FF00"));

                    linearLayout.addView(textView);
                }
            });
        }
    }




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:3)

每次按下按钮时,此示例都会添加TextView

public class MainActivity extends ActionBarActivity {

    int counter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);

        Button button = (Button) findViewById(R.id.add_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                counter++;

                TextView textView = new TextView(MainActivity.this);
                textView.setText("TextView " + counter);
                textView.setBackgroundColor(Color.parseColor("#FF0000"));
                textView.setTextColor(Color.parseColor("#00FF00"));

                linearLayout.addView(textView);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这是活动的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/add_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add TextView" />

</LinearLayout>