gpt4 book ai didi

java - 安卓 :- Separate class for handling views clicks etc;

转载 作者:太空宇宙 更新时间:2023-11-04 12:26:54 25 4
gpt4 key购买 nike

我是开发领域的新手。我正在开发 android 应用程序,在我的 Activity_main 布局中我有许多不同的项目。单击其中一个按钮后,顶部布局将替换为新布局。

现在,我不想在主类中定义新的布局按钮、textview 等,而是想要一个单独的类来初始化我的按钮等,并且在该类中我还可以声明 onClickListners。

在我的主要 Activity 中,我想要:-

public void onCreate(){

button bb = (Button)findViewById(R.id.mybutton);
View CurrentContentView= getLayoutInflater().inflate(R.layout.activity_main, null, false);
bb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MyNewViewInit(CurrentContentView);
}
});


}

在我的 MyNewViewInit() 类中:-

 public class MyNewViewInit{

ImageButton backbutton;
ChipsMultiAutoCompleteTextview search;
ImageButton searchclear;
ImageButton modeTime;
ImageButton modeTag;
TextView modeTimeTxt;
TextView modeTagTxt;
ScrollView mainScroll;
ScrollView selectedScroll;

public MyNewViewInit(View v){
backbutton = (ImageButton)v.findViewById(R.id.backbutton);
search = (ChipsMultiAutoCompleteTextview)v.findViewById(R.id.search);
searchclear = (ImageButton)v.findViewById(R.id.searchclear);
modeTime = (ImageButton)v.findViewById(R.id.modeTime);
modeTag = (ImageButton)v.findViewById(R.id.modeTag);
modeTimeTxt = (TextView)v.findViewById(R.id.modeTimeTxt);
modeTagTxt = (TextView)v.findViewById(R.id.modeTagTxt);
mainScroll = (ScrollView)v.findViewById(R.id.HAT1);
selectedScroll = (ScrollView)v.findViewById(R.id.HAT2);
tag = new OtherHelper().arrayread();
mainHashTag.setVisibility(View.GONE);
selectedHashTag.setVisibility(View.GONE);
clickListners();


}

public void clickListners(){

backbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

}
});

searchclear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

}
});

modeTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


}
});

modeTimeTxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

}
});

modeTag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

}
});

modeTagTxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


}
});


}

}

因此,当我尝试使用此代码时,onclicklistners 不起作用。

最好的方法是什么?

谢谢

最佳答案

在你的类中编写一个接口(interface),

    class MyNewViewInit{

public interface ClickListenerInterface{
void onCLickSomething(Something something);
}

ClickListenerInterface mClickListener;
...
public MyNewViewInit(View v,ClickListenerInterface clickListener){
mClickListener = clickListener;
...
}

public void clickListners(){
...
backbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mClickListener!=null){
mClickListener.onCLickSomething(v);
}else{
//throw an error that activity should implement ClickListenerInterface
}
}
});
}
}

在您的 Activity 中,

class MyActivity extends Activity implements ClickListenerInterface{
public void onCreate(){
bb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MyNewViewInit(CurrentContentView,this);
//if using Android Studio, then use the context menu to implement the interface
}
});
}

void onCLickSomething(Something something){
//do something with something
}
}

关于java - 安卓 :- Separate class for handling views clicks etc;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38283635/

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