2017-08-30 30 views
0

我有一個活動的主要和第二個活動。 活動主有3個文本視圖和一個按鈕,活動第二個有3個編輯文本一個按鈕和一個保存按鈕,第三個編輯文本是不可見的,當我按下按鈕編輯文本變得可見。如何獲取其他活動的編輯文本的可見性結果?

我的問題是,如何獲得該編輯文本的可見性狀態? 我想要顯示的結果時,它是可見的,並TextView的可見 我也希望它是無形的當次活動的按鈕未按

我在活動主當前代碼

// 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); 
     } 
    }); 
} 
+0

您檢查RESULT_OK但你的setResult的RESULT_OK_1。 – RonTLV

+0

這是一個打字錯誤。 –

回答

0

您應該將putExtra()作爲額外的結果添加到您的意圖中。你也應該用這種方式使用startActivity方法。

+0

謝謝,你可能請舉個例子嗎? –

+0

在評論中的格式化是有趣的,但我會嘗試。你應該在你的第二個活動中過去,而不是完成:Intent intent = new Intent(this,FirstActivity.class); intent.putExtra(「EditText visible」,etSize.getVisibility()); startActivity(意向); –

0

要獲得編輯文本(實際上任何視圖)的可見性,您可以使用isShown()

+0

謝謝,你可能請舉個例子嗎? –

+0

如果它是可見的,它將返回True的布爾值。所以,如果我正確理解你的問題,那麼你想檢查btn2的onClickListener來查看字段'etSize'是否可見。在這種情況下,if(etSize.isShown())'做了一些事情,然後做一些事情 – CJLWS

+0

哦,看着你的代碼,你在不同的地方聲明瞭多個對etSize的引用 - 如果你有更多的時間,你只做一次 – CJLWS

相關問題