我有十几个活动将继承针垫的相同方法。除了针垫之外,每个活动都完全不同。如何让我的活动从公共类继承方法?我做错了吗?
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("");
}
}
答案 0 :(得分:1)
无论编码方式或是否为最佳方法,为了使您的代码有效,您应该删除TestActivity中的 TextView mEntry 并设置 public 或PinPadActivity中 TextView mEntry 的 protected 修饰符(子类无法访问默认或私有变量)。这应该允许您调用onXClick()方法来设置继承自TestActivity中PinPadActivity的mEntry textview。
答案 1 :(得分:0)
这可以成功,但不是正确的方法。将PinPad变成片段。然后在每个使用它的Activity中包含Fragment。