Android按钮Onclick [英] Android Button Onclick
本文介绍了Android按钮Onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好的我是android dev和Java的新手,所以我在点击方法上遇到问题这里是我的代码。我知道我必须要亲近,提前谢谢。我希望我的按钮所做的就是,当它点击手机将布局视图从main.xml切换到xx.xml时
OK I'm new to android dev's and Java, so I'm having problems with on click method here's my code. I know I've gotta be close, thanks in advance. All I want my button to do is, when its clicked on the phone to switch the layout view from main.xml to xx.xml
package my.project;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ExperiencerlActivity extends Activity {
/** Called when the activ`enter code here`ity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
}
这是我的按钮代码
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/linearLayout1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="56dp"
android:onClick="setLogin"
android:text="Login" />
推荐答案
如果写得像这在xml文件的Button标签中: android:onClick =setLogin然后
这样做:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn"
android:onClick="onClickBtn" />
</LinearLayout>
和代码部分:
public class StartUpActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClickBtn(View v)
{
Toast.makeText(this, "Clicked on Button", Toast.LENGTH_LONG).show();
}
}
并且不需要所有这些:
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
检查一次;
这篇关于Android按钮Onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文