Android 按钮设置启用| |设置可点击不';行不通

Android 按钮设置启用| |设置可点击不';行不通,android,button,Android,Button,如果按钮的文本为null或“空”,我想将setenabled或setclickable设置为false 这是我的密码 public class SingleItemView extends Activity { // Declare Variables TextView txtoffice; TextView txttown; Button txtphone_1; Button txtphone_2; String office_name; String town_name; String ph

如果按钮的文本为null或“空”,我想将setenabled或setclickable设置为false

这是我的密码

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);
}
}
但它不起作用。当文本按钮为null或“空”时,按钮被启用并可单击


任何解决方案???

应该是这样的:

if (txtphone_2 == null || txtphone_2.getText().equals(""))
{
    //null or empty
    txtphone_2.setClickable(false);
    txtphone_2.setEnabled(false);
}
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);
}
if (txtphone_2 != null && txtphone_2.getText().equals(""))
{
    //null or empty
    txtphone_2.setClickable(false);
    txtphone_2.setEnabled(false);
}

它更容易阅读!“如果按钮不为空且文本为空……”

不客气,先生。如果您将答案标记为正确,我将不胜感激。您如何在空对象上调用
setClickable()
。@rozina您完全正确,我想过去3年没有人注意到,但我想他们仍然理解这个概念