android - (多个按钮)如何将多个 XML 文件链接到 Android 应用程序的 1 个主要 Java 页面?

标签 android android-layout

我刚开始为 acdroid 编写代码,遇到了这个问题。 显然我可以访问第一个 xml 页面(第一个 Activity ,即 newBill),但我无法访问其余 2 个 xml 页面(settleBill.class 和 Profile.class)。所有 3 个按钮的代码与您在此处看到的完全相同。 3 个独立的 java 文件 - newBill.java、settleBill.java 和 Profile.java 也完全相同。我真的需要帮助。谢谢!

public class SplitB extends Activity implements OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
   // setContentView(R.layout.settlement);

    Button newBill = (Button) findViewById(R.id.newbill);
    newBill.setOnClickListener(this);

    Button settleBill =(Button) findViewById(R.id.settlebill);
    settleBill.setOnClickListener(this);

    Button myProfile = (Button) findViewById(R.id.profile);
    myProfile.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v.getId() == R.id.newbill)
    {
        startActivity(new Intent(SplitB.this,newBill.class));
    }
    else if(v.getId() == R.id.settlebill)
    {
        startActivity(new Intent(SplitB.this,settleBill.class));
    }
    else
    {
        startActivity(new Intent(SplitB.this,Profile.class));

    }
    /*switch(v.getId())
    {
        case R.id.newbill: 
            startActivity(new Intent(SplitB.this,newBill.class));
            break;

        case R.id.settlebill:
            startActivity(new Intent(SplitB.this,settleBill.class));
            break;

        case R.id.profile:
            startActivity(new Intent(SplitB.this,settleBill.class));
            break;
    }*/
}

最佳答案

LayoutInflater in=(LayoutInflater)getLayoutInflater(savedInstanceState);
      View  v=in.inflate(R.layout.2.xml/3.xml, null);
then
    Button b=(Button)v.findviewByid(r.id.button1); we acess

关于android - (多个按钮)如何将多个 XML 文件链接到 Android 应用程序的 1 个主要 Java 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6458243/

相关文章:

android - 如何在 assembleRelease 之后执行任务?

android - Webview 中的声音

android - HttpURLConnection 运行时异常

android - Android 中填充或边距的最佳做法是什么?

android - createHorizo​​ntalChain 和 createHorizo​​ntalChainRtl 是否交换了?

android - 如何区分android中的480X800和480X854屏幕?

android - 在 Android 中开发计步器

android - React Native android 应用程序无法在发布中运行

android - 在不同设备上呈现不同的颜色

android - ActionLayout 不显示,只有它的标题可见