Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 屏幕对屏幕命令_Android_Xml_Button_Onclick_Screen - Fatal编程技术网

Android 屏幕对屏幕命令

Android 屏幕对屏幕命令,android,xml,button,onclick,screen,Android,Xml,Button,Onclick,Screen,嗨,我是android新手,已经花了10个多小时寻找这个答案,但我似乎无法找到并理解它。我在xml主页上。我试图创建一个按钮,转到另一个页面。我需要最简单的方法来做这件事。谁能帮帮我吗?这是我的主xml代码 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width=

嗨,我是android新手,已经花了10个多小时寻找这个答案,但我似乎无法找到并理解它。我在xml主页上。我试图创建一个按钮,转到另一个页面。我需要最简单的方法来做这件事。谁能帮帮我吗?这是我的主xml代码

<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" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:padding="@dimen/padding_medium"
    android:text="@string/hello_world"
    tools:context=".MainActivity" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="66dp"
    android:text="Button" />

</RelativeLayout>

有关按钮的网站上记录了此过程。谷歌搜索Android按钮

<Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="66dp"
 android:text="Button"
 android:clickable="true"
 android:onClick"insertMethodNameHere"
 />  


这将调用您在
onClick
标记中定义的方法,然后启动新活动或更新视图。无论你需要做什么,你也可以做不同的事情

您可以在活动中实例化:

private Button button;
onCreate(Bundle Bundle)
方法中,您可以找到本活动XML中定义的按钮,即您使用的
setContentView(R.layout.yourxml)

button=(button)findviewbyd(R.id.button)

然后,您可以使用:

在onClick方法中,实例化

Intent Intent=新的Intent(CurrentActivity.this、ActivityToGo.class)

CurrentActivity=您当前的活动,Activity指向您要加载的活动

startActivity()


阅读Android的基础知识。

你必须学习如何在Android中的活动/屏幕之间切换,你可以找到一个为初学者提供的详细说明教程

你可以通过访问@OvidiuLatcu发布的链接来节省时间。总是去developer.android.com,如果你没有找到答案,就来这里搜索一些问题,如果仍然没有,就发布一个问题。
<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView1" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="66dp" 
    android:text="Button" /> 
public class MyActivity extends Activity implements OnClickListener {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(this);
    }

    public void onClick(View v) {
        // Use a switch on v.getId() if you have multiple clickable views.
        // Since there's just one button, ...
        Intent intent = new Intent(this, TargetActivity.class;
        startActivity(intent);
    }
}
<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView1" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="66dp" 
    android:text="Button" /> 
Button btn=(Button)findViewById(R.id.button1);
// Register listener to button btn
btn.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

            // your action

            Intent newActivityIntent= new Intent(currentActivity.this, NewClass.class);
            startActivity(newActivityIntent);       
        }
});