我想从Main2Activity为MainActivity创建一个新的textview。我试过这个。我做错了什么?
主要活动(这与activity_main一起使用,最后意图更改为Main2Activity)
package com.example.gabriel.apppapa;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.*;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
int y,m,a,d,v,r,g;
long dr;
double idr, dida, dda;
Spinner Proyecto;
String Sproyecto;
TextView nuevo_proyecto;
MenuItem nuevo;
Button añadir;
View ll;
int dmes;
BroadcastReceiver textViewReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll = findViewById(R.id.lnlayout);
final Calendar calendar = Calendar.getInstance();
y = calendar.get(Calendar.YEAR);
m = calendar.get(Calendar.MONTH) + 1;
d = calendar.get(Calendar.DAY_OF_MONTH);
calcularColor();
cdiasmes();
nuevo = (MenuItem) findViewById(R.id.mas);
BroadcastReceiver textViewReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
addTextView();
}
};
LocalBroadcastManager.getInstance(this).registerReceiver(textViewReceiver, new IntentFilter("createtextview"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mas:
Intent next_screen = new Intent(MainActivity.this, Main2Activity.class);
startActivity(next_screen);
//finish();
default:
return super.onOptionsItemSelected(item);
}
}
public void calcularColor(){
idr = y/15.0;
dida = idr - ((int)idr);
dda = dida*255.0;
a = (int)Math.round(dda);
g = (int)Math.round(m*255.0/12.0);
r = (int)Math.round(d*255.0/28.0);
}
public void cdiasmes(){
if(m == 1||m == 3||m == 5||m == 7||m == 8||m == 10||m == 12){
dmes = 31;
}
else if (m == 4||m == 6||m == 9||m == 11){
dmes = 30;
}
else {
dmes = 28;
}
}
public void addTextView(){
TextView nproyecto = new TextView(this);
nproyecto.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
nproyecto.setBackgroundColor(Color.rgb(r,g,a));
if(((r+g+a)/3)>382.5){
nproyecto.setTextColor(Color.WHITE);
}
else {
nproyecto.setTextColor(Color.BLACK);
}
nproyecto.setText(nuevo_proyecto.getText());
((LinearLayout)ll).addView(nproyecto);
}
@Override
public void onDestroy(){
super.onDestroy();
LocalBroadcastManager.getInstance(this).unregisterReceiver(textViewReceiver);
}
} Main2Activity(从此活动开始,您可以将一个Textview(带有EditText的文本)添加到activity_main并在执行后转到主活动)
package com.example.gabriel.apppapa;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.Calendar;
public class Main2Activity extends AppCompatActivity {
int y,m,a,d,g,r,dmes;
double idr, dida, dda;
Button añadir;
View ll;
EditText nuevo_proyecto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
final Calendar calendar = Calendar.getInstance();
y = calendar.get(Calendar.YEAR);
m = calendar.get(Calendar.MONTH)+1;
d = calendar.get(Calendar.DAY_OF_MONTH);
nuevo_proyecto = (EditText) findViewById(R.id.nuevo_proyecto_nombre);
añadir = (Button) findViewById(R.id.añadir);
cdiasmes();
calcularColor();
añadir.setBackgroundColor(Color.rgb(r,g,a));
nuevo_proyecto.setHint(String.valueOf(r)+','+String.valueOf(g)+','+String.valueOf(a));
if ((r+g+a)<383) {
añadir.setTextColor(Color.WHITE);
} else {
añadir.setTextColor(Color.BLACK);
}
añadir.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LocalBroadcastManager.getInstance(Main2Activity.this).sendBroadcast(new Intent("createtextview"));
}
});
}
public void cdiasmes(){
if(m == 1||m == 3||m == 5||m == 7||m == 8||m == 10||m == 12){
dmes = 31;
}
else if (m == 4||m == 6||m == 9||m == 11){
dmes = 30;
}
else {
dmes = 28;
}
}
public void calcularColor(){
idr = y/15.0;
dida = idr - ((int)idr);
dda = dida*255.0;
a = (int)Math.round(dda);
g = (int)Math.round(m*255.0/12.0);
r = (int)Math.round(d*255.0/dmes);
}
}
答案 0 :(得分:0)
如果要在activity2中的main_activity中显示textview中的文本,那么如果在xml中创建textview并且如果它们没有要显示的数据则禁用它将非常容易。从activity2传递intent中的所需数据,并在main_activity中获取intent,检查数据并在使文本视图可见后在textview中显示数据。
答案 1 :(得分:0)
也许您可以将BroadcastReceiver与LocalBroadcastManager一起使用:
在主要活动中初始化广播接收者
package com.example.gabriel.apppapa;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.*;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
int y,m,a,d,v,r,g;
long dr;
double idr, dida, dda;
Spinner Proyecto;
String Sproyecto;
TextView nuevo_proyecto;
MenuItem nuevo;
Button añadir;
View ll;
int dmes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll = findViewById(R.id.lnlayout);
final Calendar calendar = Calendar.getInstance();
y = calendar.get(Calendar.YEAR);
m = calendar.get(Calendar.MONTH)+1;
d = calendar.get(Calendar.DAY_OF_MONTH);
calcularColor();
cdiasmes();
nuevo = (MenuItem) findViewById(R.id.mas);
//add this
BroadcastReceiver textViewReceiver= new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
addTextView()
}
};
LocalBroadcastManager.getInstance(this).registerReceiver(textViewReceiver, new IntentFilter("createtextview"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mas:
Intent next_screen = new Intent(MainActivity.this, Main2Activity.class);
startActivity(next_screen);
//finish();
default:
return super.onOptionsItemSelected(item);
}
}
public void calcularColor(){
idr = y/15.0;
dida = idr - ((int)idr);
dda = dida*255.0;
a = (int)Math.round(dda);
g = (int)Math.round(m*255.0/12.0);
r = (int)Math.round(d*255.0/28.0);
}
public void cdiasmes(){
if(m == 1||m == 3||m == 5||m == 7||m == 8||m == 10||m == 12){
dmes = 31;
}
else if (m == 4||m == 6||m == 9||m == 11){
dmes = 30;
}
else {
dmes = 28;
}
}
// move from main2activity
public void addTextView(){
TextView nproyecto = new TextView(this);
nproyecto.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
nproyecto.setBackgroundColor(Color.rgb(r,g,a));
if(((r+g+a)/3)>382.5){
nproyecto.setTextColor(Color.WHITE);
}
else {
nproyecto.setTextColor(Color.BLACK);
}
nproyecto.setText(nuevo_proyecto.getText());
((LinearLayout)ll).addView(nproyecto);*/this linear layout is from MainActivity*/
}
//add this to avoid memory leaks
@Override
public void onDestroy(){
LocalBroadcastManager.getInstance(this).unregisterReceiver(textViewReceiver);
}
}
将文本视图创建逻辑从main2activity移动到main活动,然后发出消息:
package com.example.gabriel.apppapa;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.Calendar;
public class Main2Activity extends AppCompatActivity {
int y,m,a,d,g,r,dmes;
double idr, dida, dda;
Button añadir;
View ll;
EditText nuevo_proyecto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
final Calendar calendar = Calendar.getInstance();
y = calendar.get(Calendar.YEAR);
m = calendar.get(Calendar.MONTH)+1;
d = calendar.get(Calendar.DAY_OF_MONTH);
nuevo_proyecto = (EditText) findViewById(R.id.nuevo_proyecto_nombre);
añadir = (Button) findViewById(R.id.añadir);
cdiasmes();
calcularColor();
añadir.setBackgroundColor(Color.rgb(r,g,a));
nuevo_proyecto.setHint(String.valueOf(r)+','+String.valueOf(g)+','+String.valueOf(a));
if ((r+g+a)<383) {
añadir.setTextColor(Color.WHITE);
} else {
añadir.setTextColor(Color.BLACK);
}
añadir.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// emit a message to mainactivity
LocalBroadcastManager.getInstance(Main2Activity.this).sendBroadcast(new Intent("createtextview"));
}
});
}
public void cdiasmes(){
if(m == 1||m == 3||m == 5||m == 7||m == 8||m == 10||m == 12){
dmes = 31;
}
else if (m == 4||m == 6||m == 9||m == 11){
dmes = 30;
}
else {
dmes = 28;
}
}
public void calcularColor(){
idr = y/15.0;
dida = idr - ((int)idr);
dda = dida*255.0;
a = (int)Math.round(dda);
g = (int)Math.round(m*255.0/12.0);
r = (int)Math.round(d*255.0/dmes);
}
}
答案 2 :(得分:0)
一项活动无法直接在另一项活动中创建视图。如果您使用startActivityForResults()
开始第二项活动,则可以将数据“返回”到您的主要活动。然后,主要活动将创建文本视图。谷歌上面的方法获取更多信息。
更好的是,将数据添加到文件或数据库。 MainActivity将在每次启动时读取数据并自动反映任何更改。
对于大量数据,您应该了解ListView和RecyclerView,以便轻松地将多个TextView添加到活动中。