如何在android中将数据从一个视图传递到另一个视图?

时间:2020-12-16 20:55:57

I have two views named: first_view and second_view.

我有两个名为的视图:first_view和second_view。

The first view consists of a button and an editable text view. The second view consists of a single text view.

第一个视图包含一个按钮和一个可编辑的文本视图。第二个视图由单个文本视图组成。

In my first view, I want to put a number in the datable text view. As I click the button, it should display the number in the second view.

在我的第一个视图中,我想在数据文本视图中添加一个数字。当我单击按钮时,它应该在第二个视图中显示数字。

How can I code Java classes for the two views?

如何为两个视图编写Java类?

3 个解决方案

#1


2  

I am assuming that you want to setText within the same Activity, if not so then tell me, Ill change my answer.

我假设你想在相同的Activity中设置文本,如果不是这样,那么告诉我,我改变了我的答案。

Here is what you have to do.

这是你必须做的。

public class MyActivity extends Activity {  
Button btn;
EditText et;
TextView tv;

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button)findViewById(R.id.yourbtnID);
        et = (EditText) findViewById(R.id.yourEtID);
        tv = (TextView) findViewById(R.id.yourtvID);

        btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        String myText = et.getText().toString();
        tv.setText(myText); 
    }
});
}
}

If you want to pass text between two Activities then use Intent.

如果要在两个活动之间传递文本,请使用Intent。

In your current Activity do this.

在您当前的活动中执行此操作。

Intent i = new Intent(YourCurrentActivity.this, YourNextActivity.class);
String str = yourEditText.getText().toString();
i.putExtra("edittextvalue" , str);
startActivity(i);

Then in next Activity do this..

然后在下一个Activity做这个..

Bundle extras = getIntent().getExtras(); 
String myEtText;

if (extras != null) {
myEtText = extras.getString("edittextvalue");
yourTextView.setText(myEtText);
}

#2


1  

if Two Views in the same Activity , you can do that

如果在同一个Activity中有两个视图,你可以这样做

    Button btn;
    EditText txtInput;
    TextView txtShow;
    //btn=firstView.findViewWithTag(tag)
    btn=firstView.findViewById(R.id.**);
    txtInput=firstView.findViewById(R.id.**);
    txtShow=secondView.findViewById(R.id.**);

    btn.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {

            String input=txtInput.getText().toString();
            txtShow.setText(input);
        }

    });

if you have Two Activity :

如果你有两个活动:

Button btn;
EditText txtInput;
String VALUE_KEY="show";
private void test()
{


    btn=(Button)findViewById(R.id.**);
    txtInput=(Button)findViewById(R.id.**);


    btn.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {

            String input=txtInput.getText().toString();
            Intent intent=new Intent(this, AnotherActivity.Class);
            intent.putExtra(VALUE_KEY, input);
        }

    });

}

On the AnotherActivity:

关于AnotherActivity:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent=this.getIntent();
        String value=intent.getStringExtra(VALUE_KEY);

        TextView view=(TextView)findViewById(R.id.txt);
        view.setText(value);
    }

#3


1  

Try this, put your value in String: String et_str = EditText.getText().toString();

试试这个,把你的值放在String:String et_str = EditText.getText()。toString();

When you call the other intent,

当你打电话给另一个意图时,

Intent i = new Intent(first_view .class, second_view.class);
i.putExtra("REF",et_str);
StartActivity(i);

In The Second View, get this value using getExtra()

在第二个视图中,使用getExtra()获取此值

#1


2  

I am assuming that you want to setText within the same Activity, if not so then tell me, Ill change my answer.

我假设你想在相同的Activity中设置文本,如果不是这样,那么告诉我,我改变了我的答案。

Here is what you have to do.

这是你必须做的。

public class MyActivity extends Activity {  
Button btn;
EditText et;
TextView tv;

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button)findViewById(R.id.yourbtnID);
        et = (EditText) findViewById(R.id.yourEtID);
        tv = (TextView) findViewById(R.id.yourtvID);

        btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        String myText = et.getText().toString();
        tv.setText(myText); 
    }
});
}
}

If you want to pass text between two Activities then use Intent.

如果要在两个活动之间传递文本,请使用Intent。

In your current Activity do this.

在您当前的活动中执行此操作。

Intent i = new Intent(YourCurrentActivity.this, YourNextActivity.class);
String str = yourEditText.getText().toString();
i.putExtra("edittextvalue" , str);
startActivity(i);

Then in next Activity do this..

然后在下一个Activity做这个..

Bundle extras = getIntent().getExtras(); 
String myEtText;

if (extras != null) {
myEtText = extras.getString("edittextvalue");
yourTextView.setText(myEtText);
}

#2


1  

if Two Views in the same Activity , you can do that

如果在同一个Activity中有两个视图,你可以这样做

    Button btn;
    EditText txtInput;
    TextView txtShow;
    //btn=firstView.findViewWithTag(tag)
    btn=firstView.findViewById(R.id.**);
    txtInput=firstView.findViewById(R.id.**);
    txtShow=secondView.findViewById(R.id.**);

    btn.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {

            String input=txtInput.getText().toString();
            txtShow.setText(input);
        }

    });

if you have Two Activity :

如果你有两个活动:

Button btn;
EditText txtInput;
String VALUE_KEY="show";
private void test()
{


    btn=(Button)findViewById(R.id.**);
    txtInput=(Button)findViewById(R.id.**);


    btn.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {

            String input=txtInput.getText().toString();
            Intent intent=new Intent(this, AnotherActivity.Class);
            intent.putExtra(VALUE_KEY, input);
        }

    });

}

On the AnotherActivity:

关于AnotherActivity:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent=this.getIntent();
        String value=intent.getStringExtra(VALUE_KEY);

        TextView view=(TextView)findViewById(R.id.txt);
        view.setText(value);
    }

#3


1  

Try this, put your value in String: String et_str = EditText.getText().toString();

试试这个,把你的值放在String:String et_str = EditText.getText()。toString();

When you call the other intent,

当你打电话给另一个意图时,

Intent i = new Intent(first_view .class, second_view.class);
i.putExtra("REF",et_str);
StartActivity(i);

In The Second View, get this value using getExtra()

在第二个视图中,使用getExtra()获取此值