Button btnSaveInfo = findViewById(R.id.btn_save_info); btnSaveInfo.setOnClickListener(new View.OnClickListener() {
时间: 2024-04-20 09:26:27 浏览: 182
您好!您的第一个问题是关于Android开发中的findViewById和setOnClickListener的用法。
findViewById是一个方法,用于根据给定的id查找布局文件中的视图。它需要一个参数,即要查找的视图的id。在您的代码中,R.id.btn_save_info表示要查找的视图的id是btn_save_info。
setOnClickListener是一个方法,用于为视图设置点击事件。它需要一个参数,即要执行的点击事件的监听器。在您的代码中,通过匿名内部类实现了一个View.OnClickListener接口,其中定义了onClick方法。这个方法会在按钮被点击时被触发。
所以,上述代码的作用是当btnSaveInfo按钮被点击时,会执行setOnClickListener方法中的匿名内部类中的onClick方法。您可以在onClick方法中编写您想要执行的代码,以响应按钮点击事件。
相关问题
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_estimate); Intent intent = getIntent(); String code = intent.getStringExtra("code"); String name = intent.getStringExtra("name"); final long ranking = intent.getLongExtra("ranking",0L); final float pe = intent.getFloatExtra("pe", 0.0f); final float pb = intent.getFloatExtra("pb", 0.0f); tv_code = (TextView) findViewById(R.id.tv_code); tv_name = (TextView) findViewById(R.id.tv_name); tv_ranking = (TextView) findViewById(R.id.tv_ranking); tv_pe = (TextView) findViewById(R.id.tv_pe); tv_pb = (TextView) findViewById(R.id.tv_pb); tv_suggestEstimate = (TextView) findViewById(R.id.tv_suggestEstimate); tv_code.setText("姓名:" + code); tv_name.setText("单位:" + name); tv_ranking.setText("手机号码:" + ranking); tv_pe.setText("邮件:" + pe); tv_pb.setText("备注:" + pb); btn_estimate = (Button) findViewById(R.id.btn_estimate); btn_estimate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { estimateResult = estimateData(ranking, pe, pb); //Log.i("EA", estimateResult) ; tv_suggestEstimate.setText("提示:" + estimateResult); } }); btn_return = (Button) findViewById(R.id.btn_return); btn_return.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.putExtra("data", estimateResult); setResult(2, intent); finish(); } }); }如何去掉final
如果想要去掉 `final` 关键字,需要注意以下几点:
1. `ranking`、`pe`、`pb` 这三个变量在 `onClick` 方法中被使用,如果去掉 `final`,则这三个变量如果在 `onClick` 方法中被修改,编译时会报错。
2. 如果想要去掉 `final`,则需要将这三个变量定义为 Activity 的成员变量,而不是在 `onCreate` 方法中定义。
下面是修改后的代码:
```java
private long ranking;
private float pe;
private float pb;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_estimate);
Intent intent = getIntent();
String code = intent.getStringExtra("code");
String name = intent.getStringExtra("name");
ranking = intent.getLongExtra("ranking",0L);
pe = intent.getFloatExtra("pe", 0.0f);
pb = intent.getFloatExtra("pb", 0.0f);
tv_code = (TextView) findViewById(R.id.tv_code);
tv_name = (TextView) findViewById(R.id.tv_name);
tv_ranking = (TextView) findViewById(R.id.tv_ranking);
tv_pe = (TextView) findViewById(R.id.tv_pe);
tv_pb = (TextView) findViewById(R.id.tv_pb);
tv_suggestEstimate = (TextView) findViewById(R.id.tv_suggestEstimate);
tv_code.setText("姓名:" + code);
tv_name.setText("单位:" + name);
tv_ranking.setText("手机号码:" + ranking);
tv_pe.setText("邮件:" + pe);
tv_pb.setText("备注:" + pb);
btn_estimate = (Button) findViewById(R.id.btn_estimate);
btn_estimate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
estimateResult = estimateData(ranking, pe, pb);
//Log.i("EA", estimateResult) ;
tv_suggestEstimate.setText("提示:" + estimateResult);
}
});
btn_return = (Button) findViewById(R.id.btn_return);
btn_return.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("data", estimateResult);
setResult(2, intent);
finish();
}
});
}
```
package com.example.dzy; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btnLogin,btnRegister; private ImageView ivLogin; private TextView tvUsername,tvUserId; private static final int REQUEST_REGISTER_CODE=1; private static final int REQUEST_LOGIN_CODE=2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvUserId=findViewById(R.id.tv_main_userid); tvUsername=findViewById(R.id.tv_main_username); ivLogin=findViewById(R.id.iv_login); btnLogin=findViewById(R.id.btn_main_login); btnRegister=findViewById(R.id.btn_main_register); btnLogin.setOnClickListener(this); btnRegister.setOnClickListener(this); } @Override public void onClick(View view){ Intent intent=new Intent(); switch (view.getId()){ case R.id.btn_main_login: intent.setClass(this,LoginActivity.class); startActivityForResult(intent,REQUEST_LOGIN_CODE); break; case R.id.btn_main_register: intent.setClass(this,RegisterActivity.class); startActivityForResult(intent,REQUEST_REGISTER_CODE); break; } } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent){ super.onActivityResult(requestCode,resultCode,intent); if(intent==null){ return; } switch (requestCode){ case REQUEST_REGISTER_CODE: if (resultCode==11){ tvUserId.setText(intent.getStringExtra("userId")); tvUsername.setText(intent.getStringExtra("userName")); ivLogin.setImageResource(R.mipmap.user_2); Toast.makeText(this,"注册成功!",Toast.LENGTH_LONG).show(); Intent intent1=new Intent(this,NavigationActivity.class); }else{ Toast.makeText(this,"注册失败!",Toast.LENGTH_LONG).show(); } break; case REQUEST_LOGIN_CODE: if(resultCode==21){ tvUserId.setText(intent.getStringExtra("userId")); tvUsername.setText(intent.getStringExtra("userName")); ivLogin.setImageResource(R.mipmap.user_2); Toast.makeText(this,"登录成功!",Toast.LENGTH_LONG).show(); Intent intent1=new Intent(this,NavigationActivity.class); }else{ Toast.makeText(this,"登录失败!",Toast.LENGTH_LONG).show(); } break; default: Toast.makeText(this,"操作失败!",Toast.LENGTH_LONG).show(); break; } } }这段中intent1没有进行页面跳转
是的,您是正确的。在代码中,虽然创建了一个新的 `Intent` 对象 `intent1`,但是没有将其用于页面跳转。您需要在 `case` 语句中添加页面跳转代码,例如:
```
case REQUEST_REGISTER_CODE:
if (resultCode==11){
tvUserId.setText(intent.getStringExtra("userId"));
tvUsername.setText(intent.getStringExtra("userName"));
ivLogin.setImageResource(R.mipmap.user_2);
Toast.makeText(this,"注册成功!",Toast.LENGTH_LONG).show();
Intent intent1=new Intent(this,NavigationActivity.class);
startActivity(intent1); // 添加这行代码进行页面跳转
}else{
Toast.makeText(this,"注册失败!",Toast.LENGTH_LONG).show();
}
break;
case REQUEST_LOGIN_CODE:
if(resultCode==21){
tvUserId.setText(intent.getStringExtra("userId"));
tvUsername.setText(intent.getStringExtra("userName"));
ivLogin.setImageResource(R.mipmap.user_2);
Toast.makeText(this,"登录成功!",Toast.LENGTH_LONG).show();
Intent intent1=new Intent(this,NavigationActivity.class);
startActivity(intent1); // 添加这行代码进行页面跳转
}else{
Toast.makeText(this,"登录失败!",Toast.LENGTH_LONG).show();
}
break;
```
这样,当用户注册或者登录成功时,就会跳转到 `NavigationActivity` 页面。
阅读全文