现在在第一个界面按下button之后,会跳转,但是跳转出来的界面没有应该出现的按钮button2.不知道是哪出错了,请指教!谢谢.
第一个活动
package com.example.test2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button1;
private EditText editText;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
editText=(EditText)findViewById(R.id.edit_text);
button1.setOnClickListener(this);
editText.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button1:
{
String inputText=editText.getText().toString();
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
Toast.makeText(MainActivity.this,inputText,Toast.LENGTH_SHORT).show();
break;
}
default:
break;
}
}
}
第二个活动
package com.example.test2;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
第一个XML
<?xml version="1.0" encoding="utf-8"?>
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="com.example.test2.MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type Something !"
/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:textColor="#000"
android:background="#00ff00"
android:text="Button1"
/>
第二个XML
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/button2"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:background="#00ff00"
android:textAllCaps="false"
android:text="Button2"
/>
对第二个活动的注册
已解决,
@Override
protected void onCreate(Bundle savedInstanceState){
}
和
protected void onCreate(Bundle savedInstanceState){
}
的区别.改了之后就行了
不知道是你的XML 贴上来不全,还是本来就是这样的。
并没有看到你第二个XML 中定义了< Button />.
以上
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
android:id="@+id/button2"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:background="#00ff00"
android:textAllCaps="false"
android:text="Button2"
/>