我有一个TextView
表(60 TextView),用户可以触摸它们并设置所单击的TextView
的文本和背景。
单击一个TextView
时,此事件会打开DialogActivity
startActivityForResult
并收回两个变量(字符串和颜色)。
我的问题是如何使用onActivityResult
返回的输入变量设置单击的TextView的文本和背景?
那里有一些代码 活性
package com.ddz.diarioscolastico;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class ActivitySetOrario extends ActionBarActivity {
//Static perch� cosi non perdo i dati inseriti in precedenza!
static DataBase DB = new DataBase();
static int clickedTextViewId; // Declare TextView as class level member field
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_orario);
}//Fine oncreate
//Prende indietro la materia aggiunta dall'ActivityAddMateria
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 1) {
if (resultCode == RESULT_OK) {
String result = data.getStringExtra("result"); //Take the materia from Dialog
int color = data.getIntExtra("color", 1); //Take the color from Dialog
Materia materia;
materia = new Materia();
materia.setMateria(result); //put the name materia into class materia
materia.setColor(color); //put the color materia into class materia
DB.getMaterie().add(materia);
// Now use mTextView here
//View view = findViewById(clickedTextViewId);
TextView clickedtextView = (TextView) findViewById(clickedTextViewId); //(TextView) view;
if(clickedTextViewId == clickedtextView.getId()) {
clickedtextView.setText(result);
clickedtextView.setBackgroundColor(color);
}
}
if (resultCode == RESULT_CANCELED) {
//Nessuna materia inserita
}
}
}//onActivityResult
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_set_orario, 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.
switch (item.getItemId()) {
case R.id.draw_orario:
//addMateria();
return true;
case R.id.save_data_orario:
//SERIALIZZO I DATI CHE DOVRA PRENDERE ActivityOrario
//FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
//ObjectOutputStream os = new ObjectOutputStream(fos);
//os.writeObject(this);
//os.close();
//fos.close();
backToOrario();
return true;
case R.id.exit_orario:
//Torno alla schermata orario annullo ogni modifica NON SERIALIZZO
backToOrario();
finish();
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
//Torna alla ActivityOrario
public void backToOrario(){
Intent myIntent = new Intent(ActivitySetOrario.this, ActivityOrario.class);
startActivity(myIntent);
}
public void addMateria(View v){
//To get ID of your TextView do this
clickedTextViewId = v.getId();
//StartActivityForResult perche mi aspetto la materia inserita dall'altra activity
Intent myIntent = new Intent(ActivitySetOrario.this, ActivityAddMateria.class);
ActivitySetOrario.this.startActivityForResult(myIntent, 1);
}
}
activity_set_orario.xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue_orario"
android:id="@+id/table">
<TableRow
android:id="@+id/dayrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="35dp" >
<TextView
android:id="@+id/d1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Lun."
android:textColor="@color/text_orario"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/d2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Mar."
android:textColor="@color/text_orario"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/d3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Mer."
android:textColor="@color/text_orario"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/d4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Gio."
android:textColor="@color/text_orario"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/d5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Ven."
android:textColor="@color/text_orario"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/d6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sab."
android:textColor="@color/text_orario"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<ScrollView
android:id="@+id/scrollorario"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableRow
android:id="@+id/prima_riga"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/h1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textColor="@color/text_orario"
android:textAppearance="? android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/mat11"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:gravity="center"
android:clickable="true"
android:onClick="addMateria"
android:background="@color/grigio_chiaro" />
<TextView
android:id="@+id/mat12"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:gravity="center"
android:background="@color/grigio_chiaro"
android:text="" />
<TextView
android:id="@+id/mat13"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:gravity="center"
android:background="@color/grigio_chiaro"
android:text="" />
<TextView
android:id="@+id/mat14"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:gravity="center"
android:background="@color/grigio_chiaro"
android:text="" />
<TextView
android:id="@+id/mat15"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:gravity="center"
android:background="@color/grigio_chiaro"
android:text="" />
<TextView
android:id="@+id/mat16"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:gravity="center"
android:background="@color/grigio_chiaro"
android:text="" />
</TableRow>
<TableRow
android:id="@+id/seconda_riga"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/h2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:textColor="@color/text_orario"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/mat21"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:gravity="center"
android:background="@color/grigio_chiaro"
android:text="" />
<TextView
android:id="@+id/mat22"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:gravity="center"
android:background="@color/grigio_chiaro"
android:text="" />
<TextView
android:id="@+id/mat23"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:gravity="center"
android:background="@color/grigio_chiaro"
android:text="" />
<TextView
android:id="@+id/mat24"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:gravity="center"
android:background="@color/grigio_chiaro"
android:text="" />
<TextView
android:id="@+id/mat25"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:gravity="center"
android:background="@color/grigio_chiaro"
android:text="" />
<TextView
android:id="@+id/mat26"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:gravity="center"
android:background="@color/grigio_chiaro"
android:text="" />
</TableRow>
<TableRow
android:id="@+id/terza_riga"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/h3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:textColor="@color/text_orario"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="@+id/quarta_riga"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/h4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:textColor="@color/text_orario"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="@+id/quinta_riga"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/h5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:textColor="@color/text_orario"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="@+id/sesta_riga"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/h6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:textColor="@color/text_orario"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="@+id/settima_riga"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/h7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:textColor="@color/text_orario"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="@+id/ottava_riga"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/h8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:textColor="@color/text_orario"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="@+id/nona_riga"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/h9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:textColor="@color/text_orario"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="@+id/decima_riga"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/h10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10"
android:textColor="@color/text_orario"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
</LinearLayout>
答案 0 :(得分:0)
实际上,您必须从addMateria(View v)
维护TextView的ID或使用直接TextView
。
有许多方法可以保持它,类级变量等,
比如,将TextView
声明为类级成员字段。
public class ActivitySetOrario extends ActionBarActivity {
//Static perch� cosi non perdo i dati inseriti in precedenza!
static DataBase DB = new DataBase();
int clickedTextViewId; // Declare TextViewId as class level member field
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_orario);
}/
现在,在addMateria(View v)
public void addMateria(View v){
//To get ID of your TextView do this
if(v instanceOf TextView)
{
clickedTextViewId = v.getId();
}
//StartActivityForResult perche mi aspetto la materia inserita dall'altra activity
Intent myIntent = new Intent(ActivitySetOrario.this, ActivityAddMateria.class);
ActivitySetOrario.this.startActivityForResult(myIntent, 1);
}
再次访问mTextView
中的onActivityResult()
,以更新颜色和文字
//Prende indietro la materia aggiunta dall'ActivityAddMateria
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK){
String result = data.getStringExtra("result"); //Take the materia from Dialog
int color = data.getIntExtra("color",1); //Take the color from Dialog
Materia materia;
materia = new Materia();
materia.setMateria(result); //put the name materia into class materia
materia.setColor(color); //put the color materia into class materia
DB.getMaterie().add(materia);
// Now use mTextView here
try
{
View view = findViewById(clickedTextViewId);
if(view instanceOf TextView)
{
TextView textView = (TextView) view;
textView .setText(result);
textView .setBackgroundColor(color);
}
}catch(Exception ex)
{
ex.printStacktrace();
}
}
if (resultCode == RESULT_CANCELED) {
//Nessuna materia inserita
}
}//onActivity