如何在其他活动中获得编辑文本的可见性结果?

时间:2017-08-30 18:30:34

标签: android

我有活动主要和活动第二。 Activity main有3个文本视图和一个按钮,Activity second有3个编辑文本,一个按钮和一个保存按钮,第三个编辑文本是不可见的,当我按下按钮时,编辑文本变为可见。

我的问题是,如何获取该编辑文本的可见性状态? 我希望在可见时显示结果并使textview可见 我还希望在未按下第二个活动中的按钮时它不可见

我在Activity main中的当前代码

// This method opens the second activity
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from activity.xml
    setContentView(R.layout.activity_main);
    // Locate the button in activity_main.xml
    btn1 = (Button)findViewById(R.id.Open_Form);
    // Capture button clicks
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // Start SecondActivity.class for result
            Intent myIntent3 = new Intent(MainActivity.this,
                    SecondActivity.class);
            startActivityForResult(myIntent3, ACTIVITY_RESULT_CODE);
        }
    });
// This method is called when second activity finishes
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // Check that it is the second activity with an OK result
if (requestCode == ACTIVITY_RESULT_CODE) {
        if (resultCode == RESULT_OK) {
            // Get string data from Intent
            String Brand = data.getStringExtra("@id/etBrand");
            // Set text view with string
            TextView tvBrand = (TextView)findViewById(R.id.tvBrand);
            tvBrand.setText(Brand);
            tvBrand.getVisibility();{
                String Name = data.getStringExtra("@id/etName");
                TextView tvName = (TextView)findViewById(R.id.tvName);
                tvName.setText(Name);
                tvName.getVisibility();{
                    String Size = data.getStringExtra("@id/etSize");
                    TextView tvSize = (TextView)findViewById(R.id.tvSize);
                    tvSize.setText(Size);
                    tvSize.getVisibility();{
                }
            }
        }
    }
}

我的第二个活动代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rod_dialog1);
EditText etSize = (EditText)findViewById(R.id.etSize);
    etSize.setVisibility(View.INVISIBLE);
Button button2 = (Button)findViewById(R.id.Add_Size);
    button2.setVisibility(View.VISIBLE);
// Create the submit button
    btn1 = (Button)findViewById(R.id.bSave);
    btn1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            // Get the text from EditText and put the string to pass back into an Intent
            EditText etBrand = (EditText)findViewById(R.id.etBrand);
            String stringToPassBack = etBrand.getText().toString();
            Intent myIntent1 = getIntent();
            myIntent1.putExtra("@id/etBrand", stringToPassBack);
            // Get the text and put the string to pass back into an Intent
            EditText etName = (EditText)findViewById(R.id.etName);
            String stringToPassBack1 = etName.getText().toString();
            Intent myIntent2 = getIntent();
            myIntent2.putExtra("@id/etName", stringToPassBack1);
            // Get the text and put the string to pass back into an Intent
            EditText etSize = (EditText)findViewById(R.id.etSize);
            String stringToPassBack2 = etSize.getText().toString();
            Intent myIntent3 = getIntent();
            myIntent3.putExtra("@id/etSize", stringToPassBack2);

            // Close activity
            setResult(RESULT_OK_1, myIntent1);
            finish();
        }
    });
    btn2 = (Button)findViewById(R.id.Add_Size);
    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            EditText etSize = (EditText)findViewById(R.id.etSize);
            etSize.setVisibility(View.VISIBLE);
            Button button2 = (Button)findViewById(R.id.Add_Size);
            button2.setVisibility(View.INVISIBLE);
        }
    });
}

2 个答案:

答案 0 :(得分:0)

你应该将putExtra()作为额外的结果导入你的意图。你也应该这样使用startActivity方法。

答案 1 :(得分:0)

要获得编辑文本(实际上是任何视图)的可见性,您可以使用isShown()