单击Button时没有任何反应,除非EditText为空

时间:2015-12-26 17:09:35

标签: android android-intent

我是Android的新手,在编辑文本视图中遇到问题需要你的帮助。我只是想在继续下一个意图之前检查EditText。如果EditText被至少1个字符串填充,那么它将转到下一个意图。可能这对你来说很简单,但这对我来说很难,而且我浪费了半天。这里的任何人都可以告诉我这段代码中的错误在哪里。

提前谢谢

    package com.example.tricknearn;

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;

public class Posttittle extends AppCompatActivity {
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_posttittle);
            final EditText tittleInput = (EditText) findViewById(R.id.tittleInput);
            Button postTittleButton = (Button) findViewById(R.id.postTittleButton);
            postTittleButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    if (tittleInput.getText().length() == 0) {
                        tittleInput.setError("Please enter some text");
                    }
                }
            });
        }

        public void postTittleClick(View view) {


            EditText tittleInput = (EditText) findViewById(R.id.tittleInput);
            Intent i = new Intent(this, Postdescription.class);

            if (tittleInput.getText().toString().equals("")){

            }else{

                startActivity(i);
            }
        }
    }

3 个答案:

答案 0 :(得分:0)

请尝试使用此代码...

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="packagesToScan" value="net.therap.domain.tmp"/>
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

答案 1 :(得分:0)

if (tittleInput.getText().length() == 0)替换为if (tittleInput.getText().toString.equals(""))

答案 2 :(得分:0)

postTittleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    if (tittleInput.getText().toString().isEmptry()) {
        //edittext is empty
        tittleInput.setError("Please enter some text");
    } else {
        //Edittext is not empty
        //start new Activity 
    }
  }
});

并删除postTittleClick方法