Android按钮不起作用点击单击

时间:2016-03-09 18:17:48

标签: android

我是Android新手,我正在开发一个需要登录和注册的简单项目。 当我点击链接链接到另一个活动时,按钮不起作用。

这是我的代码......

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_login"
android:gravity="center"
android:padding="10dp"
tools:context=".LoginActivity">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:weightSum="1">

    <ImageView android:src="@drawable/logo_bg"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginBottom="100dp"
        android:layout_gravity="center_horizontal"

        android:contentDescription="@string/Zilwacom_Logo" />

    <TextView android:id="@+id/login"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="24dp"
        android:text="@string/welcome_to_zilwacom"
        android:gravity="center"
        android:textSize="24sp"
        android:layout_weight="0.14" />

    <!--  Email Label -->

    <EditText
        android:id="@+id/loginemail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:hint="@string/Email" />


    <!--  Password Label -->

    <EditText
        android:id="@+id/loginpassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="@string/Password"/>


    <Button
        android:id="@+id/btnlogin"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_marginBottom="24dp"
        android:padding="12dp"
        android:text="@string/Login"/>


    <TextView
        android:id="@+id/linksignup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dip"
        android:background="@null"
        android:text="@string/btn_link_to_register"
        android:textAllCaps="false"
        android:textColor="@color/white"
        android:textSize="15dp"
        android:clickable="true"/>
    </LinearLayout>

 </LinearLayout>

和我的登录代码 当用户点击注册时,应用程序会打开注册活动

 public class LoginActivity extends AppCompatActivity {

private EditText email;
private EditText password;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);


    //Get reference to buttons and texts

    //buttons
    Button loginbtn =(Button) findViewById(R.id.btnlogin);

    TextView signuplink=(TextView) findViewById(R.id.linksignup);

    //Texts
    email=(EditText) findViewById(R.id.loginemail);
    password=(EditText) findViewById(R.id.loginpassword);

     signuplink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(LoginActivity.this, SignupActivity.class);

            startActivity(i);
           }
        });

    }

}

2 个答案:

答案 0 :(得分:1)

我设法做到了, 问题在于清单...... 我做了如下改动..

Home

  FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction()
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
            .replace(R.id.content_main, HomeFragment)
            .commit();

Expenses

FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction()
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
            .replace(R.id.content_main, ExpensesFragment)
            .commit();


Category

  FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction()
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
            .replace(R.id.content_main, CategoryFragment)
            .commit();

答案 1 :(得分:0)

添加/更改代码:

loginbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(LoginActivity.this, SignupActivity.class);

            startActivity(i);
           }
        });

    }

onClickListener设为Button [loginbtn]而不是TextView [signuplink]。

在你的xml布局文件中添加到TextView android:clickable =&#34; true&#34;