如何在android中的onclick函数中获取String值?

时间:2012-06-21 10:58:28

标签: android string onclick

   final RadioGroup radioGroup1 = (RadioGroup) findViewById(R.id.radiogroup);
              final TextView tv=(TextView) findViewById(R.id.textView1);   
        Button  mSubmitButton = (Button) findViewById(R.id.btnSubmit);
        relation.setOnItemSelectedListener(new CustomOnItemSelectedListener());
        radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        String text1;
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) 
            {
                RadioButton checkedRadioButton = (RadioButton) findViewById(checkedId);
               text1 = checkedRadioButton.getText().toString();

                Toast.makeText(getApplicationContext(), text1, Toast.LENGTH_SHORT).show();

            }
        });


        mSubmitButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                 String fName = first_name.getText().toString();
                 String re_lation = (String) relation.getSelectedItem(); 
                 String add_ress = address.getText().toString();
                  }

在我的Onclick函数中,我想从onCheckedChanged.how获取text1值以获取myclick函数中的text1值?

3 个答案:

答案 0 :(得分:3)

将String text1声明为全局变量,例如,活动的onCreate()方法之前

String text1;

    @Override
    public void onCreate()
    {

    }

答案 1 :(得分:0)

String text1;作为全局变量:

public class YourActvity extends Actvity {

   String text1;

   public onCreate( //...   

}

答案 2 :(得分:0)

我认为你需要在下面初始化text1到类初始化,所以任何方法都可以使用这个text1然后在一个方法中设置值并将值转换为另一个方法

public YourActvity extends Actvity {
String text1;
public method1(
set the value
}

public method1(
get the value
}