按钮setenabled || setclickable不起作用

时间:2014-03-01 17:47:05

标签: android button

如果按钮文本为空或“空”

,我想将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);
}
}

但是当文本按钮为空或“”为空时

按钮启用并且可单击时它不起作用

任何解决方案???

1 个答案:

答案 0 :(得分: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);
}

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