-1

在我的应用程序中有两个编辑框,当单击适当的编辑框时,它将显示一个列表视图(这是另一个意图。从该用户必须选择 1 个输入,我将这些值分配给文本框。我的问题是当我将值设置为 1编辑框并单击另一个文本框以获取第一个文本框值不存在的值。

我的代码是:

带有编辑文本框的屏幕:

 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;
            }
        });

列表视图屏幕:

对于第一个编辑框:

  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);
              }
            });        
        }   
4

2 回答 2

2

您应该使用startActivityForresult()方法来启动另一个活动

例如:在 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;
        }
    });

在 secondActivity 如果你想发回数据

Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);     
finish();

如果您不想返回数据

Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);        
finish();

onActivityResult()现在在您的 FirstActivity 类中为方法编写以下代码

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
于 2012-05-02T11:38:17.440 回答
0

尝试

meditfrom.getText().toString();
于 2012-05-02T11:28:50.193 回答