简体   繁体   English

Android,XML视图文件和Java后端,如何使用后端Java创建自定义视图?

[英]Android, XML view file and backend in Java, how can i create my custom view with backend java?

As we have main.xml file for view and another file java in src folder which contains the events, i have made another view, that onclicking a button in main.xml it will open the other view xml which should have a java backend file, when i create my custom view xml, then its backened file doesnot create, and how can i click a button and open a new view with another form, just like we do in web pages and how can our custom view have a java backend file. 由于我们有用于查看的main.xml文件和包含事件的src文件夹中的另一个文件java,所以我创建了另一个视图,单击main.xml中的按钮将打开另一个应具有Java后端文件的视图xml,当我创建自定义视图xml时,不会创建其备份文件,以及如何单击按钮并使用其他表单打开新视图,就像我们在网页中所做的一样,自定义视图如何具有Java后端文件。

Thanks Atif 谢谢阿蒂夫

XML FILES XML文件

  1. firstXML.xml
  2. secondXML.xml

FILES 档案

firstActivity.java

public class firstActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.firstXML);
    }
}

secondActivity.java

public class secondActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondXML);
    }
}

Now if you have Button in firstXML . 现在,如果您在firstXML具有Button You want this Button to launch secondActivity: 您希望此按钮启动secondActivity:

firstActivity.java

public class firstActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.firstXML);
        final Button button = (Button) findViewById(R.id.button_id);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent myIntent = new Intent(firstActivity.this,secondActivity.class);
                startActivityForResult(myIntent, 0);
            }
        });
    }
}

Some extra notes: Now if you want firstActivity to send some info to secondActivity 一些额外的注意事项:现在,如果您希望firstActivity将一些信息发送给secondActivity

Change 更改

Intent myIntent = new Intent(firstActivity.this,secondActivity.class);
startActivityForResult(myIntent, 0);

to

Intent myIntent = new Intent(firstActivity.this,secondActivity.class);
Bundle muBundle = new Bundle(); //create a Bundle
myBundle.putString("username","Sherif");
myBundle.putInt("userid",1234);
// EXAMINE THE [Bundle Class][2]
myIntent.putExtras(myBundle); //PUT THE Bundle you created in the Intent
startActivityForResult(myIntent, 0);

To capture this data in the secondActivity secondActivity捕获此数据

public class secondActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondXML);
        Bundle myBundle = this.getIntent().getExtras(); //You got the bundle
        //TO USE THE BUNDLE
        String A = myBundle.getString("username"); // A = "Sherif"
        int B = myBundle.getInt("userid"); // B = 1234
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM