hier ist mein Code für main.xml
<merge xmlns:Android="http://schemas.Android.com/apk/res/Android">
<RelativeLayout
Android:id="@id/container"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent"
xmlns:Android="http://schemas.Android.com/apk/res/Android">
<include layout="@layout/tabs" />
<ScrollView
Android:fillViewport="true"
Android:scrollbars="@null"
Android:layout_height="fill_parent"
Android:layout_width="fill_parent">
<LinearLayout
Android:paddingTop="10dp"
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:orientation="vertical" >
<!-- first text_view -->
<TextView
Android:background="@color/grey"
Android:textColor="@color/white"
Android:text="@string/category"
Android:id="@+id/category1"
Android:layout_height="wrap_content"
Android:layout_width="fill_parent"
Android:layout_marginTop="65dp"
Android:textSize="17dp"
Android:typeface="serif"/>
<!-- first horizontal_scrollview -->
<HorizontalScrollView
Android:scrollbars="@null"
Android:id="@+id/horizontalScrollView1"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content">
<LinearLayout Android:id="@+id/linearLayout1"
Android:orientation="horizontal"
Android:visibility="visible"
Android:layout_height="wrap_content"
Android:layout_width="wrap_content">
<!-- image_view should be here -->
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
</merge>
hier ist mein Code für tabs.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:orientation="horizontal"
Android:background="#333333">
<TextView
Android:textColor="@color/gradient_green"
Android:id="@+id/viewall"
Android:layout_width="85dp"
Android:layout_height="25dp"
Android:layout_marginLeft="10dp"
Android:layout_alignParentLeft="true"
Android:layout_alignParentTop="true"
Android:textSize="17dp"
Android:textStyle="bold"
Android:text="@string/view_all"
Android:onClick="onClick"
Android:focusable="false"
Android:clickable="true" />
<TextView
Android:textColor="@color/white"
Android:id="@+id/pic"
Android:layout_width="45dp"
Android:layout_height="25dp"
Android:layout_alignParentTop="true"
Android:layout_toRightOf="@+id/viewall"
Android:textSize="17dp"
Android:textStyle="bold"
Android:text="@string/pic"
Android:onClick="onClick"
Android:focusable="false"
Android:clickable="true" />
</RelativeLayout>
und hier ist der Code in der Main.Java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView all = (TextView) this.findViewById(R.id.viewall);
TextView pic = (TextView) this.findViewById(R.id.pic);
all.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TextView all = (TextView) findViewById(R.id.viewall);
TextView pic = (TextView) findViewById(R.id.pic);
Toast.makeText(Main.this, "VIEW ALL", Toast.LENGTH_SHORT).show();
all.setTextColor(getResources().getColorStateList(R.color.gradient_green));
pic.setTextColor(getResources().getColorStateList(R.color.white));
}
});
pdf.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TextView all = (TextView) findViewById(R.id.viewall);
TextView pic = (TextView) findViewById(R.id.pic);
Toast.makeText(Main.this, "VIEW ALL", Toast.LENGTH_SHORT).show();
all.setTextColor(getResources().getColorStateList(R.color.white));
pic.setTextColor(getResources().getColorStateList(R.color.gradient_green));
}
});
}
wenn ich also setContentView () in der Main.class oder Main.Java als setContentView (R.layout.tabs) anstelle von setContentView (R.layout.main) festlege, funktioniert onClick (). Was soll ich tun oder was? stimmt nicht mit meinem Code, der die Funktion von onClick () behindert?
Benutze diese
all = (TextView) this.findViewById(R.id.viewall);
pdf = (TextView) this.findViewById(R.id.pic);
in on erstellen und dann einstellen
all.setOnclickListener (this) auch in der Methode oncreate () .Implementiere onClicklistener, wenn ein Fehler angezeigt wird. es wird wie ein Zauber wirken.
Bearbeitet
TextView btn=(TextView) findViewById(R.id.accInfo);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//DO you work here
}
});
Wenn Sie Clicklistenner auf TextView setzen, wird es automatisch anklickbar, sodass keine Notwendigkeit besteht.
Android:clickable="true"
Es gibt einen einfachen Weg. Fügen Sie dies in die Textansicht in XML ein:
Android:clickable="true"
hinzufügen Android:clickable="true"
im <TextView>
In Ihrer onCreate-Methode müssen Sie:
Und Ihre Klasse muss auch OnClickListener implementieren.
public class SqliteTestsActivity extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView all = (TextView) R.findViewById(R.id.viewall);
all.setOnClickListener(this);
}
public void onClick(View v) {
// Fill in this with your switch statement
}
}