2014-03-01 88 views
-1

我想的setEnabled或setclickable爲false,如果一個按鈕上的文字爲空或「」空button setenabled || setclickable不起作用

這是我的代碼

public class SingleItemView extends Activity { 
// Declare Variables 
TextView txtoffice; 
TextView txttown; 
Button txtphone_1; 
Button txtphone_2; 
String office_name; 
String town_name; 
String phone_number_01; 
String phone_number_02; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.singleitemview); 
    // Retrieve data from MainActivity on item click event 
    Intent i = getIntent(); 
    // Get the results of Office Name 
    office_name = i.getStringExtra("office_name"); 
    // Get the results of Town Name 
    town_name = i.getStringExtra("town_name"); 
    // Get the results of Phone And Fax Numbers 
    phone_number_01 = i.getStringExtra("phone_number_01"); 
    phone_number_02 = i.getStringExtra("phone_number_02"); 
    // Locate the TextViews and Buttons in singleitemview.xml 
    txtoffice = (TextView) findViewById(R.id.office_name); 
    txttown = (TextView) findViewById(R.id.town_name); 
    txtphone_1 = (Button) findViewById(R.id.phone_number_01); 
    txtphone_2 = (Button) findViewById(R.id.phone_number_02); 
    // Load the results into the TextViews 
    txtoffice.setText(office_name); 
    txttown.setText(town_name); 
    txtphone_1.setText(phone_number_01); 
    txtphone_2.setText(phone_number_02); 
    // Hide call_02 if phone_number_02 is null 
    if(txtphone_2 != null || !txtphone_2.getText().equals("")) 
     { 
     // not null not empty 
     }else { 
     //null or empty 
      txtphone_2.setClickable(false); 
      txtphone_2.setEnabled(false); 
      } 

} 
public void call_01(View v) { 
    String strTelNo_1 = txtphone_1.getText().toString(); 
    Intent intent1 = new Intent("android.intent.action.CALL"); 
    Uri data1 = Uri.parse("tel:" + strTelNo_1); 
    intent1.setData(data1); 
    startActivity(intent1); 
} 
public void call_02(View v) { 
    String strTelNo_2 = txtphone_2.getText().toString(); 
    Intent intent2 = new Intent("android.intent.action.CALL"); 
    Uri data2 = Uri.parse("tel:" + strTelNo_2); 
    intent2.setData(data2); 
    startActivity(intent2); 
} 
} 

,但它不工作的按鈕被激活當文本按鈕爲空或「」空時可點擊

任何解決方案???

回答

1

它應該是這樣的:

if (txtphone_2 == null || txtphone_2.getText().equals("")) 
{ 
    //null or empty 
    txtphone_2.setClickable(false); 
    txtphone_2.setEnabled(false); 
} 

它更容易閱讀! 「如果按鈕爲空或文本爲空...

編輯2017: 它應該是這樣的:

if (txtphone_2 != null && txtphone_2.getText().equals("")) 
{ 
    //null or empty 
    txtphone_2.setClickable(false); 
    txtphone_2.setEnabled(false); 
} 

它更容易閱讀! 「如果該按鈕不爲空且文本爲空...

+0

謝謝你男人它工作正常 – user3328733

+0

不客氣先生。如果您將答案標爲正確,我將不勝感激。 – Merlevede

+0

如何在空對象上調用'setClickable()'? – rozina

相關問題