【问题标题】:Base Activity Button Click's not working in child activities基本活动按钮单击在子活动中不起作用
【发布时间】:2014-06-18 19:06:39
【问题描述】:

我是安卓新手。在我的应用中,有共同的标题。

我已经创建了通用 header.xml 并将该 header.xml 包含在所有活动中。

但是按钮点击 [listener] 不起作用。

如何解决问题。这是代码。

Header.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutHeader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

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

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

  <Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.3"
    android:text="History" />

  </LinearLayout>

主页.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

   <include
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
       layout="@layout/header" />

     <EditText
      android:id="@+id/txtOTPCode"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:ems="10"
      android:inputType="textPersonName" />


     <Button
      android:id="@+id/btnVerify"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="verify" />

     </LinearLayout>

BaseHeaderActivity.java

  import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
 import android.widget.Button;

  public abstract class BaseHeaderActivity extends Activity 
  {
Button btn1, btn2, btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.header);

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

    btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // Programming stuf
            //show the another view
            System.out.println("Home button Action");
        }
    });
}

}

首页 Activity.java

      import android.os.Bundle;
import android.widget.Button;

public class HomeActivity extends BaseHeaderActivity
{
Button btnVerify;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);

    btnVerify = (Button)findViewById(R.id.btnVerify);
}
}

基本活动按钮点击不起作用..! 请提出解决方案。

提前致谢, 阿伦

【问题讨论】:

    标签: android android-layout android-activity


    【解决方案1】:

    您不必为此创建两个不同的活动。创建一个活动并类似地使用所有按钮(包括 header.xml 中的按钮)

    public class MainActivity extends Activity {
     Button btn1,btnVerify;
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button)findViewById(R.id.button1);
    
        btn1.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
                // Programming stuf
                //show the another view
                Toast.makeText(MainActivity.this, "Home button Action", Toast.LENGTH_SHORT).show();
            }
        });
    
        btnVerify = (Button)findViewById(R.id.btnVerify);
        btnVerify.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
                // Programming stuf
                //show the another view
                Toast.makeText(MainActivity.this, "Button Verify Action", Toast.LENGTH_SHORT).show();
            }
        });
      }
     }
    

    您不能仅将一个布局 xml 文件与单个活动一起使用。

    注意 - 如果您仍想创建两个不同的活动 - 一个基本活动和一个子活动,那么您需要在基本活动中使用 setContentView() 方法与主布局文件(在您的情况下为 Home.xml)并使用子活动扩展它.请记住,在这种情况下,不要在子活动中使用 setContentView() 方法。除非我们想创建多个相同活动的子活动,否则这种情况通常不是首选。

    【讨论】:

      猜你喜欢
      • 2021-06-18
      • 2013-07-26
      • 1970-01-01
      • 1970-01-01
      • 2016-06-22
      • 1970-01-01
      • 1970-01-01
      • 2017-12-07
      相关资源
      最近更新 更多