Android 如何将编辑文本值传递给适配器类

Android 如何将编辑文本值传递给适配器类,android,android-intent,Android,Android Intent,我想将下面活动中的文本值传递给TextAdapter类。 公共类SecondActivity扩展了活动{ EditText et1; TextView t1,t2; Button b1,b2; String result; Context context = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(

我想将下面活动中的文本值传递给TextAdapter类。
公共类SecondActivity扩展了活动{

EditText et1;
TextView t1,t2;
Button b1,b2;
String result;
Context context = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sec);
et1 = (EditText) this.findViewById(R.id.editText1);
t1 = (TextView) this.findViewById(R.id.textView1);
t2 = (TextView) this.findViewById(R.id.textView2);
b1 = (Button) this.findViewById(R.id.button1);
b2 = (Button) this.findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t2.setText(et1.getText());
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(v.getContext(),ThirdActivity.class);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
这些值将添加到数组中 公共类TextAdapter扩展了PagerAdapter{

    private String[] textViewer = new String[]{
"Hai"--------------(Here i want the edit text value)
};
public TextAdapter (Context context){
this.context= context;
}

@Override
public int getCount() {
return textViewer.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view==((TextView)object);
}
public Object instantiateItem(ViewGroup container, int position) {
TextView textView = new TextView(context);
textView.setText(textViewer[position]);
((ViewPager) container).addView(textView, 0);
return textView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((TextView) object);
}

}

在活动中创建上下文,并在PageAdapter类中传递上下文,如下所示:

public class MainActivity extends Activity {

public EditText editText = null;
private Context context = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 editText = (EditText)findViewById(R.id.xxxx);

 context = this;

 AnotherClass anotherClassObject = new AnotherClass(context)
}
public class AnotherClass extends PageAdapter {
public AnotherClass(Context context) 
    {
       context.editText.getText().toString().trim();  // Here you will get the edittext value
    }
}
其中,页面适配器类类似于以下内容:

public class MainActivity extends Activity {

public EditText editText = null;
private Context context = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 editText = (EditText)findViewById(R.id.xxxx);

 context = this;

 AnotherClass anotherClassObject = new AnotherClass(context)
}
public class AnotherClass extends PageAdapter {
public AnotherClass(Context context) 
    {
       context.editText.getText().toString().trim();  // Here you will get the edittext value
    }
}
它可能会要求您键入cast上下文,在这种情况下,可以使用活动类名键入cast

更新:-

公开编辑文本并调用

((SecondActivity)context).et1.getText().toString().trim();

在活动中创建上下文并在PageAdapter类中传递上下文,如下所示:

public class MainActivity extends Activity {

public EditText editText = null;
private Context context = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 editText = (EditText)findViewById(R.id.xxxx);

 context = this;

 AnotherClass anotherClassObject = new AnotherClass(context)
}
public class AnotherClass extends PageAdapter {
public AnotherClass(Context context) 
    {
       context.editText.getText().toString().trim();  // Here you will get the edittext value
    }
}
其中,页面适配器类类似于以下内容:

public class MainActivity extends Activity {

public EditText editText = null;
private Context context = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 editText = (EditText)findViewById(R.id.xxxx);

 context = this;

 AnotherClass anotherClassObject = new AnotherClass(context)
}
public class AnotherClass extends PageAdapter {
public AnotherClass(Context context) 
    {
       context.editText.getText().toString().trim();  // Here you will get the edittext value
    }
}
它可能会要求您键入cast上下文,在这种情况下,可以使用活动类名键入cast

更新:-

公开编辑文本并调用

((SecondActivity)context).et1.getText().toString().trim();

另请注意,请始终使您的问题形象化,以便社区人员为您提供好的答案。您可能会说您尝试过这件事,但在这件事上失败。使用代码支持它。提出直接问题将否定您的观点,人们会犹豫您是否提供好的答案。嘿,现在检查更新的答案。请始终注意让你的问题变得形象化,以便社区人员为你提供好的答案。你可能会说你尝试过这个东西,但失败了。用你的代码支持它。直接提问会否定你的观点,人们会犹豫你是否提供好的答案。嘿,现在检查更新的答案。我如何传递一个实例this:如何传递此的实例: