【问题标题】:How to get visibility result of edit text on other activity?如何在其他活动上获得编辑文本的可见性结果?
【发布时间】:2017-08-30 18:30:33
【问题描述】:

我有一个主要活动和第二个活动。 Activity main 有 3 个文本视图和一个按钮,Activity 第二个有 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);
        }
    });
}

【问题讨论】:

  • 您检查了 RESULT_OK,但您为 RESULT_OK_1 设置了结果。
  • 这是一个打字错误。

标签: android


【解决方案1】:

您应该将 putExtra() 附加到您的意图中。你也应该这样使用 startActivity 方法。

【讨论】:

  • 谢谢,能不能给个例子?
  • 在 cmets 中格式化是 afwul 但我会尝试。您应该在第二个活动中过去而不是完成: Intent intent = new Intent(this, FirstActivity.class); intent.putExtra("EditText 是否可见", etSize.getVisibility());开始活动(意图);
【解决方案2】:

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

【讨论】:

  • 谢谢,能不能给个例子?
  • 如果可见,则返回一个布尔值 True。因此,如果我正确理解了您的问题,那么您想检查 btn2 的 onClickListener 以查看字段“etSize”是否可见。在这种情况下,按照if (etSize.isShown()) 的方式做一些事情
  • 哦,看看你的代码,你在不同的地方声明了对 etSize 的多个引用——如果你只做一次,你会轻松很多
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-04
  • 1970-01-01
  • 2015-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-23
相关资源
最近更新 更多