2016-01-21 138 views
0

我正在尝试开发一个应用程序Android 5.0.1 on Eclipse。我面临一些奇怪的事情,我已经找到了他们,但没有找到我找到的解决方案。 基本上,在本次活动:onClick方法不能正常工作

package esp1415.xyz; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.Locale; 
import android.app.Activity; 
import android.content.Intent; 
import android.media.RingtoneManager; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.EditText; 
import android.widget.TableRow; 
import android.widget.TextView; 
import android.widget.TimePicker; 

public class Sveglia extends Activity { 

private DBHelper dbh = new DBHelper(this); 
private DettagliFarmaco detfar; 

private EditText ednome; 
private EditText eddesc; 
private EditText edqnt; 
private EditText edind; 
private TimePicker tp; 
private CheckBox chksett; 
private CheckBox chklun; 
private CheckBox chkmar; 
private CheckBox chkmer; 
private CheckBox chkgio; 
private CheckBox chkven; 
private CheckBox chksab; 
private CheckBox chkdom; 
private TextView titsuon; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.alarm); 
    getActionBar().setTitle("Inserisci i valori"); 

    ednome = (EditText) findViewById(R.id.ednome); 
    eddesc = (EditText) findViewById(R.id.eddesc); 
    edqnt = (EditText) findViewById(R.id.edqnt); 
    edind = (EditText) findViewById(R.id.edind); 
    tp = (TimePicker) findViewById(R.id.tp); 
    chksett = (CheckBox) findViewById(R.id.chksett); 
    chklun = (CheckBox) findViewById(R.id.chklun); 
    chkmar = (CheckBox) findViewById(R.id.chkmar); 
    chkmer = (CheckBox) findViewById(R.id.chkmer); 
    chkgio = (CheckBox) findViewById(R.id.chkgio); 
    chkven = (CheckBox) findViewById(R.id.chkven); 
    chksab = (CheckBox) findViewById(R.id.chksab); 
    chkdom = (CheckBox) findViewById(R.id.chkdom); 
    titsuon = (TextView) findViewById(R.id.titolo_suoneria); 

    long id = getIntent().getExtras().getLong("id"); 

    if (id == -1) { 
     detfar = new DettagliFarmaco(); 
    } else { 
     detfar = dbh.getFarmaco(id); 

     ednome.setText(detfar.nome); 
     eddesc.setText(detfar.descrizione); 
     edqnt.setText(detfar.quantità); 
     edind.setText(detfar.indice_associato); 
     tp.setCurrentMinute(detfar.minuti); 
     tp.setCurrentHour(detfar.ore); 
     chksett.setChecked(detfar.ripsettimana); 
     chklun.setChecked(detfar.getRipGiorno(DettagliFarmaco.LUNEDÌ)); 
     chkmar.setChecked(detfar.getRipGiorno(DettagliFarmaco.MARTEDÌ)); 
     chkmer.setChecked(detfar.getRipGiorno(DettagliFarmaco.MERCOLEDÌ)); 
     chkgio.setChecked(detfar.getRipGiorno(DettagliFarmaco.GIOVEDÌ)); 
     chkven.setChecked(detfar.getRipGiorno(DettagliFarmaco.VENERDÌ)); 
     chksab.setChecked(detfar.getRipGiorno(DettagliFarmaco.SABATO)); 
     chkdom.setChecked(detfar.getRipGiorno(DettagliFarmaco.DOMENICA)); 
     titsuon.setText(RingtoneManager.getRingtone(this, detfar.suoneria).getTitle(this)); 

     TextView contenitoreSuoneria = (TextView) findViewById(R.id.suoneria); 
     contenitoreSuoneria.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); 
      startActivityForResult(intent , 1); 
     } 
     }); 

    Button sal = (Button) findViewById(R.id.salva); 
    sal.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      salvataggio(); 

      TextView data = (Button) findViewById(R.id.data); 
      String d = new SimpleDateFormat("dd-MM-YYYY", Locale.ITALY).format(new Date()); 
      data.setText(d); 

      TextView ora = (Button) findViewById(R.id.ora); 
      String o = new SimpleDateFormat("HH:mm", Locale.ITALY).format(new Date()); 
      ora.setText(o); 

      DBHelper dbh = new DBHelper(Sveglia.this); 
      if (detfar.id < 0) { 
       dbh.createFarmaco(detfar); 
      } else { 
       dbh.updateFarmaco(detfar); 
      } 
      Intent in1 = new Intent(view.getContext(), ListaFarmaciActivity.class); 
      startActivityForResult(in1, 0); 

     } 

});} 
} 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (resultCode == RESULT_OK) { 
     switch (requestCode) { 
      case 1: { 
       detfar.suoneria = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); 
       titsuon.setText(RingtoneManager.getRingtone(this, detfar.suoneria).getTitle(this)); 
       break; 
      } 
      default: { 
       break; 
      } 
     } 
    } 
} 

private void salvataggio() { 

    detfar.nome = ednome.getText().toString(); 
    detfar.descrizione = eddesc.getText().toString(); 
    detfar.quantità = edqnt.getText().toString(); 
    detfar.indice_associato = edind.getText().toString(); 
    detfar.minuti = tp.getCurrentMinute().intValue(); 
    detfar.ore = tp.getCurrentHour().intValue(); 
    detfar.ripsettimana = chksett.isChecked(); 
    detfar.setRipGiorno(detfar.LUNEDÌ, chklun.isChecked()); 
    detfar.setRipGiorno(detfar.MARTEDÌ, chkmar.isChecked()); 
    detfar.setRipGiorno(detfar.MERCOLEDÌ, chkmer.isChecked()); 
    detfar.setRipGiorno(detfar.GIOVEDÌ, chkgio.isChecked()); 
    detfar.setRipGiorno(detfar.VENERDÌ, chkven.isChecked()); 
    detfar.setRipGiorno(detfar.SABATO, chksab.isChecked()); 
    detfar.setRipGiorno(detfar.DOMENICA, chkdom.isChecked()); 
    detfar.attiva = true; 
} 
} 

为TextView的contenitoreSuoneria的onclick方法不起作用。视图是可点击的,但没有任何反应。另外,我为XML中的活动设置的背景没有出现在我用于测试的手机上(Samsung Galaxy Note 4)。

的XML代码:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:isScrollContainer="true" 
    android:background="@drawable/bg8" > 

    <TableLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:shrinkColumns="*" 
    android:stretchColumns="*"> 

     <TextView 
      android:id="@+id/nome" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/nome" 
      android:textColor="#FAEBD7" /> 

     <EditText 
      android:id="@+id/ednome" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:backgroundTint="#00FFFF" 
      android:ems="10" 
      android:hint="@string/h1" 
      android:inputType="text" 
      android:textColor="#FAEBD7" /> 

     <TextView 
     android:id="@+id/desc" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="@string/desc" 
     android:textColor="#FAEBD7" /> 

     <EditText 
     android:id="@+id/eddesc" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:inputType="text" 
     android:ems="10" 
     android:textColor="#FAEBD7" 
     android:backgroundTint="#00FFFF" 
     android:hint ="@string/h2"/> 


     <TextView 
     android:id="@+id/qnt" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="@string/qnt" 
     android:textColor="#FAEBD7" /> 

     <EditText 
     android:id="@+id/edqnt" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:inputType="text" 
     android:textColor="#FAEBD7" 
     android:backgroundTint="#00FFFF" 
     android:ems="10" 
     android:hint ="@string/h3" /> 

     <TextView 
     android:id="@+id/ind" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="@string/ind" 
     android:textColor="#FAEBD7" /> 

     <EditText 
     android:id="@+id/edind" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:inputType="text" 
     android:ems="10" 
     android:textColor="#FAEBD7" 
     android:backgroundTint="#00FFFF" 
     android:hint ="@string/h4"/> 


    <TimePicker 
     android:id="@+id/tp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:timePickerMode="clock" /> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/sett" 
      android:textColor="#FAEBD7"/> 

     <CheckBox 
      android:id="@+id/chksett" 
      style="android:checkboxStyle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="34dp" 
      android:buttonTint="#FAEBD7"/> 

     </TableRow> 
     <View 
      android:id="@+id/div1" 
      style="@style/linea" /> 


    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/lun" 
      android:textColor="#FAEBD7"/> 

     <CheckBox 
      android:id="@+id/chklun" 
      android:layout_marginLeft="34dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:buttonTint="#FAEBD7" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/mar" 
      android:textColor="#FAEBD7"/> 

     <CheckBox 
      android:id="@+id/chkmar" 
      android:layout_marginLeft="34dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:buttonTint="#FAEBD7"/> 
     </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/mer" 
      android:textColor="#FAEBD7"/> 

     <CheckBox 
      android:id="@+id/chkmer" 
      android:layout_marginLeft="34dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:buttonTint="#FAEBD7" /> 
     </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/gio" 
      android:textColor="#FAEBD7"/> 

     <CheckBox 
      android:id="@+id/chkgio" 
      android:layout_marginLeft="34dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:buttonTint="#FAEBD7" /> 
     </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/ven" 
      android:textColor="#FAEBD7"/> 

     <CheckBox 
      android:id="@+id/chkven" 
      android:layout_marginLeft="34dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:buttonTint="#FAEBD7" /> 
     </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/sab" 
      android:textColor="#FAEBD7"/> 

     <CheckBox 
      android:id="@+id/chksab" 
      android:layout_marginLeft="34dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:buttonTint="#FAEBD7"/> 
     </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/dom" 
      android:textColor="#FAEBD7"/> 

     <CheckBox 
      android:id="@+id/chkdom" 
      android:layout_marginLeft="34dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:buttonTint="#FAEBD7"/> 
     </TableRow> 

     <View 
      android:id="@+id/div2" 
      style="@style/linea" /> 

      <TextView 
       android:id="@+id/suoneria" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/suo" 
       android:textSize="18sp" 
       android:textColor="#FAEBD7" 
       android:background="@drawable/touch_selector" 
       android:clickable="true" /> 

      <TextView 
       android:id="@+id/titolo_suoneria" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/sel" 
       android:textSize="14sp" 
       android:textColor="#FAEBD7" /> 

      <View 
      android:id="@+id/div3" 
      style="@style/linea" /> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <Button 
      android:id="@+id/salva" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/imp" 
      android:textColor="#FAEBD7" 
      android:layout_marginLeft="100dp"/> 
     </TableRow> 
</TableLayout> 
</ScrollView> 

有人能帮助我吗?非常感谢,我很抱歉我的英语。

回答

0

尝试在您的文本框中添加padding。它看起来像due to lesser content,touch不适用于textview

UPDATE 尝试setFocusable(true)textView

+0

谢谢你的回答,我试过了,但它不工作。手机上的触摸正在应用,但我无法选择铃声,因为没有任何反应。 – Velika

1

使简单

<TextView 
      android:id="@+id/suoneria" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/suo" 
      android:textSize="18sp" 
      android:textColor="#FAEBD7" 
      android:background="@drawable/touch_selector" 
      android:clickable="true" 
      android:onClick="click" /> 

内,您的活动

public void click(View view) 
{ 
// these lines executes when you click on your textview. 
} 

所有其他的东西看起来确定。请尝试。

+0

感谢您的回答。我试过了,但是现在eclipse正在说无效类型中的变量click。我应该在行getActionBar后关闭onCreate方法吗? – Velika

+0

对不起,我不能让你 –

+0

你能说清楚吗?我推荐使用Android Studio而不是eclips。 –

0

尝试在你的xml文件中使用android:onClick