1

嘿,我正在尝试使用不同的按钮来打开 android 项目中的不同页面,但只有一个按钮打开了一个新页面。

我是编程新手,所以我的术语可能不正确,但我正在关注一个 youtube 教程,它展示了如何创建一个按钮并使其打开一个新页面。我尝试为多个按钮执行此操作,但我认为我在主要活动中犯了错误。抱歉,如果我没有提供写信息来帮助我解决问题。

package test.activity.today;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle; 
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ActivityTutorialActivity extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button next = (Button) findViewById(R.id.next_button);
    next.setOnClickListener(new OnClickListener(){

        public void onClick (View v){
            Intent myIntent = new Intent(v.getContext(), NextActivity.class);
            v.getContext().startActivity(myIntent);

        }               
    });
}

public void onCreate1(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button next = (Button) findViewById(R.id.question_button);
    next.setOnClickListener(new OnClickListener(){

        public void onClick (View v){
            Intent myIntent = new Intent(v.getContext(), Question.class);
            v.getContext().startActivity(myIntent);

        }               
    });
}


public void onCreate2(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button next = (Button) findViewById(R.id.owner_cost);
    next.setOnClickListener(new OnClickListener(){

        public void onClick (View v){
            Intent myIntent = new Intent(v.getContext(), Owner.class);
            v.getContext().startActivity(myIntent);
        }
    });
    }
}
4

3 回答 3

1

你应该只有一种onCreate()方法..检查android活动的生命周期来理解它

package test.activity.today;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle; 
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ActivityTutorialActivity extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button next = (Button) findViewById(R.id.next_button);
next.setOnClickListener(new OnClickListener(){

    public void onClick (View v){
        Intent myIntent = new Intent(v.getContext(), NextActivity.class);
        v.getContext().startActivity(myIntent);

    }               
});
Button question = (Button) findViewById(R.id.question_button);
question.setOnClickListener(new OnClickListener(){

    public void onClick (View v){
        Intent myIntent = new Intent(v.getContext(), Question.class);
        v.getContext().startActivity(myIntent);

    }               
});
Button ownerCost = (Button) findViewById(R.id.owner_cost);
ownerCost.setOnClickListener(new OnClickListener(){

    public void onClick (View v){
        Intent myIntent = new Intent(v.getContext(), Owner.class);
        v.getContext().startActivity(myIntent);
    }
});
}

}
于 2012-08-29T00:08:24.260 回答
0

您正在复制该onCreate()方法...此方法由 Android 本地调用,因此,您的其他方法都不会被调用(如果它们被调用,您会遇到其他问题)。要创建多个按钮,您需要在布局中添加新按钮,然后将它们添加到onCreate().

于 2012-08-29T00:10:28.953 回答
0

还有另一种实现 onClick 的方法。

在您的布局中,您可以指定调用 onClick 的函数

<ImageButton
    android:id="@+id/imageButtonNext1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:background="@color/back_color"
    android:onClick="RegistraterCompanyOnClick"
    android:src="@drawable/ic_next" />

然后在您的活动中,您可以按其 ID 过滤哪个按钮。见下文。

public void RegistraterCompanyOnClick(View v){

    switch(v.getId()){
    case R.id.imageButtonNext1:
        String cname = company.getText().toString();

        if (cname.length()== 0){

            message = getString(R.string.company_required);

            ShowDialog(message);

        }
        else{
            company_name =  company.getText().toString();

            VerifyClient(company_name);
        }

        break;
    case R.id.imageButtonInfo1:
        //message = getString(R.string.registration_info);

        message = "Device ID:\n" + deviceID;

        // TODO Auto-generated method stub
        ShowDialog(message);

        break;
    case R.id.imageButtonHelp1:

        message = getString(R.string.registration_contact);

        // TODO Auto-generated method stub

        ShowDialog(message);
        break;

    case R.id.imageButtonPrevious1:
        Intent resultIntent = new Intent();

        // TODO Auto-generated method stub

        resultIntent.putExtra("company_name",               company.getText().toString());
        resultIntent.putExtra("company_id", companyID);
        resultIntent.putExtra("location_name", location_name);
        resultIntent.putExtra("location_id", locationID);

        setResult(Activity.RESULT_CANCELED, resultIntent);

        finish();
    default:
        break;
    }


}
于 2012-08-29T02:40:25.197 回答