【问题标题】:hide button if text length is < 1 in android studio如果在android studio中文本长度<1,则隐藏按钮
【发布时间】:2016-12-28 01:59:18
【问题描述】:

我是 Android 和 Java 新手,正忙于编辑现有应用。如果没有从 Web 服务中提取任何文本,我正在尝试隐藏一个按钮。当文本被填充时,我已经使它与另一个按钮一起工作

if (textEvent.length() > 1) {
    buttonEventSetup.setVisibility(View.INVISIBLE);
}

但是当我使用时:

if (textEvent.length() < 1) {
    buttonAccessControl.setVisibility(View.INVISIBLE);
}

似乎什么都没有发生。

我不知道代码 sn-p 是在错误的地方还是其他东西覆盖了代码。这是我的活动代码:

package com.example.dsouchon.myapplication;
    public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button buttonEventSetup =    (Button)findViewById(R.id.buttonEventSetup);

    if(Local.isSet(getApplicationContext(), "LoggedIn"))
    {
        String loggedInUser = Local.Get(getApplicationContext(), "LoggedIn");
        if(loggedInUser.length()>0)
        {
            buttonEventSetup.setVisibility(View.VISIBLE);
        }
        else
        {
            buttonEventSetup.setVisibility(View.GONE);
        }
    }
    else
    {
        buttonEventSetup.setVisibility(View.GONE);
    }

    if(Local.isSet(getApplicationContext(), "EventName"))
    {
        String event =  Local.Get(getApplicationContext(), "EventName");
        TextView textEvent = (TextView) findViewById(R.id.textEventName);
        textEvent.setText( event);
        Button buttonAccessControl = (Button)findViewById(R.id.buttonAccessControl);
        buttonAccessControl.setEnabled(true);

        //HIDES SET EVENT BUTTON WHEN EVENT IS SET
        if (textEvent.length() > 1) {
            buttonEventSetup.setVisibility(View.INVISIBLE);
        }
        if (textEvent.length() < 1) {
            buttonAccessControl.setVisibility(View.INVISIBLE);
        }
    }
    else
    {
        Button buttonAccessControl = (Button)findViewById(R.id.buttonAccessControl);
        buttonAccessControl.setEnabled(false);
    }

    if(Local.isSet(getApplicationContext(), "EventImage"))
    {
        TextView textEvent = (TextView) findViewById(R.id.textEventName);
        String result =  Local.Get(getApplicationContext(), "EventImage");

        ImageView imageViewEventImage = (ImageView)findViewById(R.id.imageViewEventImage);
        byte[] decodedString = Base64.decode(result, Base64.DEFAULT);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        imageViewEventImage.setImageBitmap(decodedByte);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main2, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void setupEvent(View view) {
    Intent intent = new Intent(MainActivity.this, SetupEvent.class );
    finish();
    startActivity(intent);
}

public void accessControl(View view) {
    Button buttonEventSetup = (Button)findViewById(R.id.buttonEventSetup);
    buttonEventSetup.setVisibility(View.GONE);

    Intent intent = new Intent(MainActivity.this, MainActivity21.class );
    finish();
    startActivity(intent);
}

public void Logoff(View view) {
    Local.Set(getApplicationContext(), "LoggedIn", "");
}

public void Login(View view) {
    final AlertDialog ad=new AlertDialog.Builder(this).create();
    MySOAPCallActivity cs = new MySOAPCallActivity();
    try {
        EditText userName = (EditText) findViewById(R.id.editUserName);
        EditText password = (EditText) findViewById(R.id.editPassword);
        String user = userName.getText().toString();
        String pwd = password.getText().toString();
        LoginParams params = new LoginParams(cs, user, pwd);

        Local.Set(getApplicationContext(), "UserName", user);
        Local.Set(getApplicationContext(), "Password", pwd);

        new CallSoapLogin().execute(params);
       // new CallSoapGetCurrentEvents().execute(params);

    } catch (Exception ex) {
        ad.setTitle("Error!");
        ad.setMessage(ex.toString());
    }
    ad.show();
}

public class CallSoapLogin extends AsyncTask<LoginParams, Void, String> {

    private Exception exception;

    @Override
    protected String doInBackground(LoginParams... params) {
        return params[0].foo.Login(params[0].username, params[0].password);
    }

    protected void onPostExecute(String result) {
        // TODO: check this.exception
        // TODO: do something with the feed
        try {

        TextView loginResult =(TextView)findViewById(R.id.labelLoginResult);
        loginResult.setVisibility(View.VISIBLE);
        loginResult.setText(result);

       // Button buttonUnsetEvent = (Button)findViewById(R.id.buttonUnsetEvent);
       // buttonUnsetEvent.setEnabled(true);

        //Spinner spinner2 = (Spinner)findViewById(R.id.spinner2);
        //spinner2.setEnabled(true);

        boolean LoginSuccessful = false;

        if(result.toLowerCase().contains("success"))
        {
            LoginSuccessful = true;
        }

        if (LoginSuccessful)
        {
            String user = Local.Get(getApplicationContext(), "UserName");
            Local.Set(getApplicationContext(), "LoggedIn", user);
            LinearLayout layoutLoggedIn = (LinearLayout)findViewById(R.id.layoutLoggedIn);
            layoutLoggedIn.setVisibility(View.VISIBLE);

            Button buttonEventSetup = (Button)findViewById(R.id.buttonEventSetup);
            buttonEventSetup.setVisibility(View.VISIBLE);

            LinearLayout layoutLogIn = (LinearLayout)findViewById(R.id.layoutLogIn);
            layoutLogIn.setVisibility(View.VISIBLE);

        }
        } catch (Exception ex) {
           String e3 = ex.toString();
        }
    }
}
private static class LoginParams {
    MySOAPCallActivity foo;
    String username;
    String password;

    LoginParams(MySOAPCallActivity foo, String username, String        password) {
        this.foo = foo;
        this.username = username;
        this.password = password;

       }
     }
}

【问题讨论】:

  • 您是否尝试过调试以检查您用于隐藏按钮的代码是否到达?
  • textEvent 是一个 textView,而不是一个字符串,textEvent.getText().toString(); 否则你会得到它的任何大小,因为它返回一个 iD,除非它的 null
  • 哦,textevent是一个textview,没注意! @Charuka 是对的,这就是问题所在
  • 那么我将如何解决这个问题。对不起。我是新手

标签: java android android-activity findviewbyid


【解决方案1】:

你得到的 Textview 的长度不起作用。使用文本长度。这应该工作

   String event =  Local.Get(getApplicationContext(), "EventName");
    //HIDES SET EVENT BUTTON WHEN EVENT IS SET
    if (event.length() > 1) {
        buttonEventSetup.setVisibility(View.VISIBLE);
    }
    if (event.length() < 1) {
        buttonAccessControl.setVisibility(View.INVISIBLE);
    }

【讨论】:

    【解决方案2】:

    在您上面的代码中,textEvent 不是字符串值。它是TextView 类型的对象。 length() 在这种情况下不会返回该元素中包含的文本的长度。您需要先显式获取文本字符串,然后才能获取它的长度。这是使用TextView 上的getText() 方法获取的。

    您的代码应如下所示:

    //HIDES SET EVENT BUTTON WHEN EVENT IS SET
            if (textEvent.getText().length() >= 1) {
                buttonEventSetup.setVisibility(View.VISIBLE);
            }
            if (textEvent.getText().length() < 1) {
                buttonAccessControl.setVisibility(View.INVISIBLE);
            }
    

    在您发布的代码示例中,您在这两种情况下也都有INVISIBLE

    请注意,您的代码也不处理文本长度 == 1 的情况。您可以使用以下内容简化整个块。

    注意:任何值 != 0 都被归类为真

    buttonEventSetup.setVisibility(textEvent.getText().length() ? View.VISIBLE : View.INVISIBLE);
    

    【讨论】:

      猜你喜欢
      • 2020-10-08
      • 2020-11-06
      • 2014-05-03
      • 2022-08-11
      • 1970-01-01
      • 1970-01-01
      • 2015-07-11
      • 2016-07-29
      • 1970-01-01
      相关资源
      最近更新 更多