Java 如何使用android studio创建两个android按钮
我刚刚进入android应用程序,还没有找到一个详细说明如何操作的教程。有人能告诉我如何在android中创建两个按钮(登录和注册)的代码吗Java 如何使用android studio创建两个android按钮,java,android,xml,Java,Android,Xml,我刚刚进入android应用程序,还没有找到一个详细说明如何操作的教程。有人能告诉我如何在android中创建两个按钮(登录和注册)的代码吗 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); loginButton=(Button)findV
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loginButton=(Button)findViewById(R.id.button);
button.setOnClickListener(LogInListener);
signUpButton=(Button)findViewById(R.id.button2);
button2.setOnClickListener(SignUpListener);
}
private OnClickListener LogInListener=new OnClickListener()
{
public void onClick(View v)
{
}
}
这是正确的实施方式吗?谢谢
活动\u main.xml
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log In"
android:id="@+id/button"
android:layout_marginTop="61dp"
android:layout_below="@+id/textView3"
android:layout_toStartOf="@+id/button2"
android:layout_toLeftOf="@+id/button2" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign UP"
android:id="@+id/button2"
android:layout_alignTop="@+id/button"
android:layout_alignLeft="@+id/editText2"
android:layout_alignStart="@+id/editText2"
android:layout_marginLeft="48dp"
android:layout_marginStart="48dp" />
编辑:
现在您已经编辑了问题,只需再做一件事,将按钮声明为实例变量。在所有方法(onCreate)之外但在mainActivity内部声明它们
预编辑:
我将向您展示您的主要活动(Java类)和布局(XML文件)的外观:
主要活动: findViewById方法继承自AppCompatActivity类,所有活动都扩展了AppCompatActivity类。旧版本的android只是扩展了Activity类 findViewById方法接受一个int参数,更确切地说是一个id 之所以需要强制转换,是因为findViewById方法(您假定该方法返回一种视图类型)将被强制转换为按钮
XML布局文件:
/>
在上面的代码中,我用两种方式表示按钮的文本
1)硬编码字符串“登录”
2)String资源“@String/signUpText
将硬编码字符串更改为后一种格式是一种很好的做法。要创建按钮,您必须在xml文件中编码,或者在设计视图中拖动它,这样做对您有好处 这将自动神奇地为你做这件事,自动生成的值 -要命名按钮,请编辑android:text -edit android:id编辑将xml中的按钮设计连接到java的“键” 如果您想将按钮用于onclicklisteners之类的东西,您需要将其“导入”到java代码中,就像这样。android:id=“@/value”和findViewById(R.id.)应该是相同的(尽管它不在照片中)
现在,只需对您想要的每个按钮再次执行此操作,并根据您的需要更改值。希望我能提供帮助。如果您是
Android Development
的新手,有些事情会让您感到困惑。我会这样做来创建按钮:
XML文件中定义按钮
侦听器
添加到按钮
id
属性添加到您的按钮
<Button
android:id="@+id/buttonOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button One" />
<Button
android:id="@+id/buttonTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
findViewById(int)方法试图膨胀的xml在哪里?如果你没有,那么你需要做的就是用Java创建button对象。@Seng让我知道这是否有帮助谢谢。这帮了我很多,因为我不知道从哪里开始制作一个简单的按钮。谢谢你的帮助:)
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="@+id/signInButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign In"
<!-- Complete Layout Details--> />
<Button
android:id="@+id/signUpButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/signUpText"
<!-- Complete Layout Details--> />
</RelativeLayout>
<Button
android:id="@+id/buttonOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button One" />
<Button
android:id="@+id/buttonTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
public class MainActivity extends AppCompatActivity implements View.onClickListener {
private Button buttonOne;
private Button buttonTwo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonOne = (Button) findViewById(R.id.buttonOne); // id located in your xml file
buttonOne.setOnClickListener(this);
buttonTwo = (Button) findViewById(R.id.buttonTwo);
buttonTwo.setOnCliclListener(this);
}
private void onClick(View v){
switch(v.getId()) {
case r.id.buttonOne: {
// action when buttonOne is clicked
break;
}
case r.id.buttonTwo: {
// action when buttonTwo is clicked
break;
}
}
}