【问题标题】:I want to work with two button in Android Studio我想在 Android Studio 中使用两个按钮
【发布时间】:2015-11-19 12:37:42
【问题描述】:

我是 Android Studio 的初学者。我想使用 2 按钮,但它总是出现错误。我正在使用另一台计算机,所以我无法向您发送代码视图。 如果你有关于这个主题的示例代码,你能帮我吗?

【问题讨论】:

标签: android button android-studio


【解决方案1】:

这里是简单的按钮使用方法。

在布局文件中

 <Button
       android:id="@+id/button1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Button 1"/>

<Button
       android:id="@+id/button2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Button 2"/>

定义按钮

Button btn1 = (Button)findViewById(R.id.button1);
Button btn2 = (Button)findViewById(R.id.button2);

按钮的点击事件

btn1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
});

btn2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

【讨论】:

  • 这是我会发布的。 +1
【解决方案2】:

这直接来自 android 文档。链接在这里: http://developer.android.com/reference/android/widget/Button.html

     public class MyActivity extends Activity {
     protected void onCreate(Bundle icicle) {
     super.onCreate(icicle);

     setContentView(R.layout.content_layout_id);

     final Button button = (Button) findViewById(R.id.button_id);
     button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             // Perform action on click
         }
     });
   }
 }

所以你使用“findViewById”方法从 xml 中检索一个按钮。或者您可以通过像这样初始化按钮以编程方式添加视图

Button bt = new Button(this); //"this" if inside a view or activity class or        you have to get context somehow
ViewGroup.LayoutParams vl = new ViewGroup.LayoutParams(height, width);
bt.setLayoutParams(vl);

ViewGroup vg = new ViewGroup(this);
vg.addView(bt);

【讨论】:

    【解决方案3】:

    试试这个:

    第一步 - 使用两个按钮创建 XML 布局

    <?xml version="1.0" encoding="utf-8"?>
    <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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="net.napples.myapplication.MainActivity">
    
        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="BUTTON 1"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="BUTTON 2"
            android:layout_below="@+id/button1"
            android:layout_centerHorizontal="true"
            />
    
    </RelativeLayout>
    

    第二步 - 为活动创建一个 java 类

    public class MainActivity extends AppCompatActivity {
    
        /* UI Views */
        Button mButton1;
        Button mButton2;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Get UI references
            findViewsById();
    
            // Set listener for the Views (like ImageView, TextView, Button, etc)
            setListenerForViews();
        }
    
        private void findViewsById() {
    
            try {
                /* Get a reference for each button */
                mButton1 = (Button) findViewById(R.id.button1);
                mButton2 = (Button) findViewById(R.id.button2);
    
            } catch (NullPointerException exc) {
                exc.printStackTrace();
            }
    
        }
    
        private void setListenerForViews() {
    
            mButton1.setOnClickListener(myListener);
            mButton2.setOnClickListener(myListener);
    
        }
    
        View.OnClickListener myListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // See Step 3
            }
        };
    }
    

    第三步 - 创建一个监听器来管理每个按钮的点击

    View.OnClickListener myListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (view == mButton1)
                /* Your code, for example start a new Activity */
                startActivity(this, SecondActivity.class);
            if (view == mButton2) 
                /* Your code, for example set text for a TextView */
                mTextView1.setText("New text");
        }
    };
    

    【讨论】:

      【解决方案4】:

      感谢您的所有回答。我试图解决使用两个按钮编写文本的问题。但我是这样解决的;

          final TextView text= (TextView)findViewById(R.id.text);
          final TextView text2=(TextView)findViewById(R.id.text2);
          Button buton2= (Button)findViewById(R.id.buton2);
          Button buton=(Button)findViewById(R.id.buton);
          buton.setOnClickListener(new View.OnClickListener() {
      
              @Override
              public void onClick(View v) {
                  text.setText("Hi!");
              }
          });
      
          buton2.setOnClickListener(new View.OnClickListener() {
      
              @Override
              public void onClick(View v) {
                  text2.setText("Whats's up?");
              }
          });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-23
        • 1970-01-01
        • 1970-01-01
        • 2021-02-02
        • 2013-01-03
        • 2021-10-17
        相关资源
        最近更新 更多