获取Edittext值

时间:2012-05-02 11:26:09

标签: android android-edittext

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

我的代码是:

使用edittext框进行屏幕:

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

ListView屏幕:

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

2 个答案:

答案 0 :(得分: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();

现在,在FirstActivity类中,为onActivityResult()方法

编写以下代码
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

答案 1 :(得分:0)

meditfrom.getText().toString();