네 개의 버튼이있는 main.xml을 만든 다음 Activity를 만들었습니다. 여기서 첫 번째 버튼이 새로운 활동을 열 수있는 인 텐트를 작성하고 작동했습니다. 그런데 두 번째 버튼도 똑같이하고 싶었지만 첫 번째 버튼과 같지 않았습니다. 내 대답은 왜입니까? 같은 행동을 취해야하는 이유는 무엇입니까?두 번째 버튼이 작동하지 않지만 첫 번째 버튼은
MainActivity.java<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/noua" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/zece" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/unspe" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/doispe" />
</LinearLayout>
: 여기서
package com.orar;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
//implement the OnClickListener interface
public class MainActivity extends Activity
implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get the Button reference
//Button is a subclass of View
//buttonClick if from main.xml "@+id/buttonClick"
View btnClick = findViewById(R.id.button1);
//set event listener
btnClick.setOnClickListener(this);
}
//override the OnClickListener interface method
@Override
public void onClick(View arg0) {
if(arg0.getId() == R.id.button1){
//define a new Intent for the second Activity
Intent intent = new Intent(this,SecondActivity.class);
//start the second Activity
this.startActivity(intent);
}
}
번째 버튼 문제 시작 ...
여기 은 ..main.xml에 코드이며
//implement the OnClickListener interface
public class MainActivity extends Activity
implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get the Button reference
//Button is a subclass of View
//buttonClick if from main.xml "@+id/buttonClick"
View btnClick = findViewById(R.id.button2);
//set event listener
btnClick.setOnClickListener(this);
}
//override the OnClickListener interface method
@Override
public void onClick(View arg0) {
if(arg0.getId() == R.id.button2){
//define a new Intent for the second Activity
Intent intent2 = new Intent(this,ThirdActivity.class);
//start the second Activity
this.startActivity(intent2);
}
}
}
}
초 ondActivity.java :
package com.orar;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.second);
}
}
ThirdActivity.java :
package com.orar;
import android.app.Activity;
import android.os.Bundle;
public class ThirdActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.third);
}
}
second.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is the second Activity"
/>
</LinearLayout>
third.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is the third Activity"
/>
</LinearLayout>
으로 main.xml에에서 버튼의
onClick
속성을 추가 할 수 있습니다. 시도한 것을 공유하고 예상대로 작동하지 않는 것을 정확히 알려 주실 수 있습니까? – gahfy내 활동에서 두 번째 단추를 어떻게 호출해야합니까? 두 번째 버튼이 새로운 액티비티를 시작하도록 변경하려고했지만 두 번째 버튼에 대해 동일한 작업을 수행하기 위해 첫 번째 버튼의 코드에서 변경해야하는 항목은 무엇입니까? – user3104504