android studio使用按钮生成文本视图
单击按钮时是否可以生成文本视图 目前,我有一个按钮,只是得到一个按钮的idandroid studio使用按钮生成文本视图,android,android-studio,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;
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);
}
}
此示例在每次按下按钮时添加一个
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>
此示例在每次按下按钮时添加一个
文本视图
:
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>
此示例在每次按下按钮时添加一个
文本视图
:
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>
此示例在每次按下按钮时添加一个
文本视图
:
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>
可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的您对textstring是什么意思?您有什么问题?你在你的活动中看到“添加文本视图”按钮了吗?我看到了按钮,但每次我点击它时,它都没有任何作用:(我将活动更改为mainactivity(因为我的mainactivity.java被称为mainactivity),将活动样本更改为Activity\u main,因为acivitymain被称为请尝试添加TextView.setBackgroundColor(Color.parseColor(“#FF0000”);
和textView.setTextColor(Color.parseColor(“#00FF00”);
在textView.setText之后是什么意思?你有什么问题吗?你在你的活动中看到“添加textView”按钮吗?我看到了这个按钮,但每次点击它都没有任何作用:(我将活动更改为mainactivity)(因为我的mainactivity.java被称为mainactivity)而activity_sample被称为acivitymain,所以请尝试添加textView.setBackgroundColor(Color.parseColor(#FF0000”);
和textView.setTextColor(Color.parseColor(#00FF00”))
在textView.setText之后textstring是什么意思?你有什么问题吗?你在你的活动中看到“添加textView”按钮了吗?我看到了按钮,但每次我点击它时它都不起任何作用:(我将活动更改为mainactivity(因为我的mainactivity.java被称为mainactivity)和activity_sample到activity_main,因为acivitymain被称为,请尝试在textView.setBackgroundColor(Color.parseColor(“#FF0000”);
和textView.setTextColor(Color.parseColor(#00FF00”);
之后添加textView.setText
你对textstring有什么意思?你看到按钮了吗在你的活动中添加TextView?我看到了按钮,但每当我点击它时,它什么也不做:(我将活动更改为mainactivity(因为我的mainactivity.java被称为mainactivity),将Activity_sample更改为Activity_main,因为acivitymain被称为,请尝试添加TextView.setBackgroundColor(Color.parseColor(“#FF0000”))
和textView.setTextColor(Color.parseColor(“#00FF00”);
在textView.setText之后