gpt4 book ai didi

java - 如何让Activity继承方法

转载 作者:行者123 更新时间:2023-12-02 13:42:35 25 4
gpt4 key购买 nike

我有十几个 Activity 将继承密码键盘的相同方法。除了密码键盘之外,每个 Activity 都完全不同。如何让我的 Activity 继承公共(public)类的方法?我做错了什么吗?

public class TestActivity extends PinPadActivity {
TextView mEntry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
mEntry = (TextView) findViewById(R.id.entry_textview);
}
}


public class PinPadActivity extends AppCompatActivity {
String mPin;
String mPinShow;
TextView mEntry;
public void on1Click(View v) {
mPin = mPin+"1";
mPinShow = mPinShow+"*";
mEntry.setText(mPinShow);
}
public void on2Click(View v) {
mPin = mPin+"2";
mPinShow = mPinShow+"*";
mEntry.setText(mPinShow);
}
public void on3Click(View v) {
mPin = mPin+"3";
mPinShow = mPinShow+"*";
mEntry.setText(mPinShow);
}
public void on4Click(View v) {
mPin = mPin+"4";
mPinShow = mPinShow+"*";
mEntry.setText(mPinShow);
}
public void on5Click(View v) {
mPin = mPin+"5";
mPinShow = mPinShow+"*";
mEntry.setText(mPinShow);
}
public void on6Click(View v) {
mPin = mPin+"6";
mPinShow = mPinShow+"*";
mEntry.setText(mPinShow);
}
public void on7Click(View v) {
mPin = mPin+"7";
mPinShow = mPinShow+"*";
mEntry.setText(mPinShow);
}
public void on8Click(View v) {
mPin = mPin+"8";
mPinShow = mPinShow+"*";
mEntry.setText(mPinShow);
}
public void on9Click(View v) {
mPin = mPin+"9";
mPinShow = mPinShow+"*";
mEntry.setText(mPinShow);
}
public void on0Click(View v) {
mPin = mPin+"0";
mPinShow = mPinShow+"*";
mEntry.setText(mPinShow);
}
public void onClearClick(View v) {
mPin = "";
mPinShow = "";
mEntry.setText("");
}
}

最佳答案

无论编码方式或是否是最佳方法,为了使您的代码正常工作,您应该删除 TestActivity 中的 TextView mEntry 并设置 public 或PinPadActivity 中 TextView mEntryprotected 修饰符(默认或私有(private)变量无法被子类访问)。这应该允许您调用 onXClick() 方法来设置从 TestActivity 中的 PinPadActivity 继承的 mEntry TextView 。

关于java - 如何让Activity继承方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42657910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com