如何获得specyfic布局的可读 ID?
我的activity_miejsce.xml文件的片段:
<LinearLayout
android:id="@+id/elementPlace"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/layout_main_menu_selector"
android:clickable="true"
android:onClick="Place">
...
</LinearLayout>
此布局的活动:
public class PlacesActivity extends AppCompatActivity {
LinearLayout element;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_places);
element = (LinearLayout) findViewById(R.id.elementPlace);
}
public void Place(android.view.View v){
Intent place = new Intent(this, Miejsce.class);
place.putExtra("source", element.toString());
startActivity(place);
}}
Miejsce.java(我们意图的目的地):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_miejsce);
Intent src = getIntent();
String source = src.getStringExtra("source");
TextView msg = (TextView) findViewById(R.id.napis);
msg.setText(getIntent().getStringExtra("source"));
Toast.makeText(getApplicationContext(), "Source: " + source, Toast.LENGTH_SHORT).show();
}
结果(来自Toast): 来源:android.widget.LinearLayout {14e02738 V.E ... C. ... P .... 0,165-1080,375#7f0c007e app:id / elementPlace}
我想使用specyfic布局的id,因为Miejsce.java将生成一个依赖于源的布局。 例如:有3种布局(酒店,加油站,餐厅)。用户点击了其中一个(@ + id / restaurant),并且发送了id为source(触发器)的id。 Java代码生成了与餐厅的specyfic布局。
答案 0 :(得分:0)
感谢link @Jackowski。 更改后的PlacesActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_places);
}
public void Place(android.view.View v){
switch(v.getId()){
case R.id.elementPlace:
Intent place = new Intent(this, Place.class);
place.putExtra("source", "elementPlace");
startActivity(place);
break;
}
}