问题
In my application Two edit boxes are there,when clicking appropriate Editbox it will show An listview(which is another intent.From that user have to select 1 input,i am assigning these value to the textbox.Myproblem is when i set value for 1 editbox and clicking another textbox to get values the 1st textbox value is not there.
My Code is:
Screen with edittext box:
public void onCreate(Bundle savedInstanceState)
{
final String fromunit=getIntent().getStringExtra("Fromunit");
final String tounit=getIntent().getStringExtra("Tounit");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText medit=(EditText)findViewById(R.id.editText1);
EditText meditfrom=(EditText)findViewById(R.id.editText2);
Button b1=(Button)findViewById(R.id.button1);
EditText meditto=(EditText)findViewById(R.id.editText3);
meditfrom.setText(fromunit);
meditto.setText(tounit);
meditfrom.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent arg1)
Intent FromIntent= new Intent(v.getContext(),Fromlist.class);
startActivity(FromIntent);
return false;
}
});
meditto.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent arg1)
{
// TODO Auto-generated method stub
Intent ToIntent= new Intent(v.getContext(),Tolist.class);
startActivity(ToIntent);
return false;
}
});
The ListView Screen:
FOR 1st editbox:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.fromunit, FROMUNIT));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
// Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
// Toast.LENGTH_SHORT).show();
Intent prev=new Intent(view.getContext(),ServiceExampleActivity.class);
prev.putExtra("Fromunit",((TextView) view).getText());
startActivity(prev);
}
});
}
For 2nd edittextbox:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.fromunit, TOUNIT));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
// Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
// Toast.LENGTH_SHORT).show();
Intent prev=new Intent(view.getContext(),ServiceExampleActivity.class);
prev.putExtra("Tounit",((TextView) view).getText());
startActivity(prev);
}
});
}
回答1:
You should use startActivityForresult() method to start another Activity
eg: In FirstActivity
int REQUEST_CODE= 1;
public boolean onTouch(View v, MotionEvent arg1)
Intent FromIntent= new Intent(v.getContext(),Fromlist.class);
startActivityForresult(FromIntent,REQUEST_CODE);
return false;
}
});
In secondActivity if you want to send back data
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();
if you don't want to return data
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
finish();
Now in your FirstActivity class write following code for onActivityResult() method
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if(resultCode == RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == RESULT_CANCELED) {
//Write your code on no result return
}
}//onAcrivityResult
回答2:
try
meditfrom.getText().toString();
来源:https://stackoverflow.com/questions/10412784/getting-edittext-values